OAuth implemented
This commit is contained in:
committed by
GitHub
parent
8cb07ae079
commit
1f0ecd696d
+64
@@ -1,5 +1,12 @@
|
||||
import type {
|
||||
StartOAuthFlowParams,
|
||||
StartOAuthFlowReturnType,
|
||||
} from "@clerk/clerk-expo";
|
||||
import * as Linking from "expo-linking";
|
||||
import * as SecureStore from "expo-secure-store";
|
||||
|
||||
import { fetchAPI } from "./fetch";
|
||||
|
||||
export interface TokenCache {
|
||||
getToken: (key: string) => Promise<string | undefined | null>;
|
||||
saveToken: (key: string, token: string) => Promise<void>;
|
||||
@@ -32,3 +39,60 @@ export const tokenCache = {
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
type StartOAuthFlowType = (
|
||||
startOAuthFlowParams?: StartOAuthFlowParams,
|
||||
) => Promise<StartOAuthFlowReturnType>;
|
||||
|
||||
export const googleOAuth = async (startOAuthFlow: StartOAuthFlowType) => {
|
||||
try {
|
||||
const { createdSessionId, signUp, setActive } = await startOAuthFlow({
|
||||
redirectUrl: Linking.createURL("/(root)/(tabs)/home", {
|
||||
scheme: "ryde", // match with scheme in app.json
|
||||
}),
|
||||
});
|
||||
|
||||
if (createdSessionId) {
|
||||
if (setActive) {
|
||||
await setActive!({ session: createdSessionId });
|
||||
|
||||
if (signUp && signUp.createdUserId) {
|
||||
await fetchAPI("/(api)/user", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
name: `${signUp.firstName} ${signUp.lastName}`,
|
||||
email: signUp.emailAddress,
|
||||
clerkId: signUp.createdUserId,
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
code: "success",
|
||||
message: "You are logged in.",
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
code: "failed",
|
||||
message: "An error occured.",
|
||||
};
|
||||
} else {
|
||||
// Use signIn or signUp for next steps such as MFA
|
||||
}
|
||||
} catch (err) {
|
||||
console.log("[OAUTH]: ", err);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
code: (
|
||||
err as {
|
||||
code?: string;
|
||||
}
|
||||
)?.code,
|
||||
message: "Internal Server Error.",
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user