this repo has no description
1import React from 'react'
2import {
3 Text,
4 View,
5 StyleSheet,
6 Button,
7 Alert,
8 TextInput,
9 Platform,
10} from 'react-native'
11import {
12 digest,
13 getRandomValues,
14 createJwt,
15 generateJwk,
16 ExpoOAuthClient,
17} from 'expo-atproto-auth'
18import { OAuthSession } from '@atproto/oauth-client'
19import { Agent } from '@atproto/api'
20import type { ExpoKey } from 'expo-atproto-auth'
21
22const client = new ExpoOAuthClient({
23 clientMetadata: {
24 client_id: 'https://hailey.at/oauth-client-metadata.json',
25 client_name: 'React Native OAuth Client Demo',
26 client_uri: 'https://hailey.at',
27 redirect_uris: ['at.hailey:/auth/callback'],
28 scope: 'atproto transition:generic',
29 token_endpoint_auth_method: 'none',
30 response_types: ['code'],
31 grant_types: ['authorization_code', 'refresh_token'],
32 application_type: 'native',
33 dpop_bound_access_tokens: true,
34 },
35 handleResolver: 'https://bsky.social',
36})
37
38export default function App() {
39 const [values, setValues] = React.useState<Uint8Array>()
40 const [sha, setSha] = React.useState<Uint8Array>()
41 const [jwt, setJwt] = React.useState<string>()
42 const [privateJwk, setPrivateJwk] = React.useState<ExpoKey | undefined>()
43 const [session, setSession] = React.useState<OAuthSession>()
44 const [input, setInput] = React.useState<string>()
45 const [agent, setAgent] = React.useState<Agent>()
46
47 return (
48 <View style={styles.container}>
49 <Text>Current Account: {session ? session.did : 'No Account'}</Text>
50 <Text>Values: {values}</Text>
51 <Button
52 title="Generate Random Values"
53 onPress={() => {
54 const newValues = getRandomValues(400)
55 setValues(newValues)
56 }}
57 />
58 <Text>SHA: {sha}</Text>
59 <Button
60 title="SHA from values"
61 onPress={() => {
62 if (!values) {
63 return
64 }
65 let newSha: Uint8Array | undefined
66 try {
67 newSha = digest(values, 'sha256')
68 } catch (e: any) {
69 Alert.alert('Error', e.toString())
70 return
71 }
72 setSha(newSha)
73 }}
74 />
75 <Text>JWT: {jwt}</Text>
76 <Button
77 title="Create JWT"
78 onPress={() => {
79 if (!privateJwk) {
80 return
81 }
82
83 let newJwt: string | undefined
84 try {
85 newJwt = createJwt('', '', privateJwk)
86 } catch (e: any) {
87 Alert.alert('Error', e.toString())
88 return
89 }
90 setJwt(newJwt)
91 }}
92 />
93 <Text>Priv Key: {privateJwk?.kid}</Text>
94 <Button
95 title="Create JWK"
96 onPress={() => {
97 let newJwk: ExpoKey | undefined
98 try {
99 newJwk = generateJwk('ES256')
100 } catch (e: any) {
101 Alert.alert('Error', e.toString())
102 return
103 }
104 setPrivateJwk(newJwk)
105 }}
106 />
107
108 <TextInput
109 onChangeText={(t) => setInput(t)}
110 style={{ height: 40, width: 300, borderWidth: 1, padding: 5 }}
111 placeholder="Input"
112 autoCorrect={false}
113 />
114 <Button
115 title="Open Sign In"
116 onPress={async () => {
117 const res = await client.signIn(input ?? '')
118 if (res.status === 'success') {
119 setSession(res.session)
120 const newAgent = new Agent(res.session)
121 setAgent(newAgent)
122 } else if (res.status === 'error') {
123 Alert.alert('Error', (res.error as any).toString())
124 } else {
125 Alert.alert(
126 'Error',
127 `Received unknown WebResultType: ${res.status}`
128 )
129 }
130 }}
131 />
132
133 <Button
134 title="Restore from DID"
135 onPress={async () => {
136 try {
137 const restoreRes = await client.restore(input ?? '')
138 setSession(restoreRes)
139
140 const newAgent = new Agent(restoreRes)
141 setAgent(newAgent)
142 } catch (e: any) {
143 Alert.alert('Error', e.toString())
144 }
145 }}
146 />
147
148 <Button
149 title="Get Profile"
150 onPress={async () => {
151 try {
152 const res = await agent?.getProfile({
153 actor: session?.did ?? '',
154 })
155 Alert.alert(
156 'Profile',
157 `Display Name: ${res?.data.displayName}, Bio: ${res?.data.description}`
158 )
159 } catch (e: any) {
160 Alert.alert('Error', e.toString())
161 }
162 }}
163 disabled={!agent}
164 />
165
166 <Button
167 title="Create Post"
168 onPress={async () => {
169 try {
170 await agent?.post({
171 text: `Test post from Expo Atproto Auth example using platform ${Platform.OS}`,
172 })
173 } catch (e: any) {
174 Alert.alert('Error', e.toString())
175 }
176 }}
177 disabled={!agent}
178 />
179 </View>
180 )
181}
182
183const styles = StyleSheet.create({
184 container: {
185 flex: 1,
186 alignItems: 'center',
187 justifyContent: 'center',
188 },
189})