frontend client for gemstone. decentralised workplace app

refactor: provide stub native oauth

serenity c474d11c 9f92ee49

Changed files
+112 -65
src
components
lib
utils
+2 -52
src/components/Auth/Login.tsx
···
-
import { oAuthClient } from "@/lib/utils/atproto/oauth";
-
import { useSetOAuthValue } from "@/providers/OAuthProvider";
-
import { Agent } from "@atproto/api";
-
import { useState } from "react";
-
import { Button, StyleSheet, TextInput, View } from "react-native";
+
import { Text } from "react-native";
export const Login = () => {
-
const [atprotoHandle, setAtprotoHandle] = useState("");
-
const setOAuth = useSetOAuthValue();
-
-
const handlePress = async () => {
-
const session = await oAuthClient.signIn(atprotoHandle);
-
-
const agent = new Agent(session);
-
if (setOAuth)
-
setOAuth({
-
session,
-
agent,
-
});
-
};
-
-
const handleSubmit = () => {
-
handlePress()
-
.then()
-
.catch((e: unknown) => {
-
console.log(e);
-
});
-
};
-
-
return (
-
<View>
-
<TextInput
-
style={styles.input}
-
value={atprotoHandle}
-
onChangeText={setAtprotoHandle}
-
placeholder="alice.bsky.social"
-
onSubmitEditing={handleSubmit}
-
/>
-
<Button title="Log in with your PDS ->" onPress={handleSubmit} />
-
</View>
-
);
+
return <Text>Login not implemented for native yet :(</Text>;
};
-
-
const styles = StyleSheet.create({
-
input: {
-
flex: 1,
-
borderWidth: 1,
-
borderColor: "#ccc",
-
borderRadius: 8,
-
paddingHorizontal: 12,
-
paddingVertical: 8,
-
marginRight: 8,
-
fontSize: 16,
-
},
-
});
+55
src/components/Auth/Login.web.tsx
···
+
import { oAuthClient } from "@/lib/utils/atproto/oauth";
+
import { useSetOAuthValue } from "@/providers/OAuthProvider";
+
import { Agent } from "@atproto/api";
+
import { useState } from "react";
+
import { Button, StyleSheet, TextInput, View } from "react-native";
+
+
export const Login = () => {
+
const [atprotoHandle, setAtprotoHandle] = useState("");
+
const setOAuth = useSetOAuthValue();
+
+
const handlePress = async () => {
+
const session = await oAuthClient.signIn(atprotoHandle);
+
+
const agent = new Agent(session);
+
if (setOAuth)
+
setOAuth({
+
session,
+
agent,
+
});
+
};
+
+
const handleSubmit = () => {
+
handlePress()
+
.then()
+
.catch((e: unknown) => {
+
console.log(e);
+
});
+
};
+
+
return (
+
<View>
+
<TextInput
+
style={styles.input}
+
value={atprotoHandle}
+
onChangeText={setAtprotoHandle}
+
placeholder="alice.bsky.social"
+
onSubmitEditing={handleSubmit}
+
/>
+
<Button title="Log in with your PDS ->" onPress={handleSubmit} />
+
</View>
+
);
+
};
+
+
const styles = StyleSheet.create({
+
input: {
+
flex: 1,
+
borderWidth: 1,
+
borderColor: "#ccc",
+
borderRadius: 8,
+
paddingHorizontal: 12,
+
paddingVertical: 8,
+
marginRight: 8,
+
fontSize: 16,
+
},
+
});
+32
src/lib/utils/atproto/oauth.ts
···
+
import { isDevMode } from "@/lib/utils/env";
+
import type {
+
AuthorizeOptions,
+
OAuthClient,
+
OAuthSession,
+
} from "@atproto/oauth-client";
+
import type {
+
ExpoOAuthClientOptions} from "@atproto/oauth-client-expo";
+
import {
+
ExpoOAuthClient as PbcNativeExpoOAuthClient,
+
} from "@atproto/oauth-client-expo";
+
import oAuthMetadata from "../../../../assets/oauth-client-metadata.json";
+
+
// suuuuuch a hack holy shit
+
export type TypedExpoOAuthClient = new (
+
options: ExpoOAuthClientOptions,
+
) => TypedExpoOAuthClientInstance;
+
+
export interface TypedExpoOAuthClientInstance extends OAuthClient {
+
signIn(signIn: string, options?: AuthorizeOptions): Promise<OAuthSession>;
+
handleCallback(): Promise<null | OAuthSession>;
+
}
+
+
// i cast type magic
+
const ExpoOAuthClient =
+
PbcNativeExpoOAuthClient as unknown as TypedExpoOAuthClient;
+
+
export const oAuthClient = new ExpoOAuthClient({
+
// @ts-expect-error funky wunky with typey wypies
+
clientMetadata: isDevMode ? undefined : oAuthMetadata,
+
handleResolver: "https://bsky.social",
+
});
+23 -13
src/lib/utils/atproto/oauth.web.ts
···
OAuthSession,
} from "@atproto/oauth-client-browser";
import type { ExpoOAuthClientOptions } from "@atproto/oauth-client-expo";
-
import { ExpoOAuthClient as PbcExpoOAuthClient } from "@atproto/oauth-client-expo";
+
import { ExpoOAuthClient as PbcWebExpoOAuthClient } from "@atproto/oauth-client-expo";
import oAuthMetadata from "../../../../assets/oauth-client-metadata.json";
// suuuuuch a hack holy shit
···
}
// i cast type magic
-
const ExpoOAuthClient = PbcExpoOAuthClient as unknown as TypedExpoOAuthClient;
+
const ExpoOAuthClient = PbcWebExpoOAuthClient as unknown as TypedExpoOAuthClient;
export const oAuthClient = new ExpoOAuthClient({
// @ts-expect-error funky wunky with typey wypies
···
handleResolver: "https://bsky.social",
});
-
const result = await oAuthClient.init();
-
-
if (result && isDevMode) {
-
const { session, state } = result;
-
if (state != null) {
-
console.log(
-
`${session.sub} was successfully authenticated (state: ${state})`,
+
oAuthClient
+
.init()
+
.then((result) => {
+
if (result && isDevMode) {
+
const { session, state } = result;
+
if (state != null) {
+
console.log(
+
`${session.sub} was successfully authenticated (state: ${state})`,
+
);
+
} else {
+
console.log(
+
`${session.sub} was restored (last active session)`,
+
);
+
}
+
}
+
})
+
.catch((err: unknown) => {
+
console.error(
+
"something went wrong when trying to init the oauth client.",
);
-
} else {
-
console.log(`${session.sub} was restored (last active session)`);
-
}
-
}
+
console.error(err);
+
});