feat: add gitlab oauth, cleanup code

This commit is contained in:
2024-11-17 19:49:42 +07:00
parent a6cc450ea7
commit 83d0ed380d
29 changed files with 297 additions and 89 deletions
@@ -0,0 +1,43 @@
import React, { useEffect, useMemo } from "react";
import { useAuthRequest } from "expo-auth-session";
import { Button } from "tamagui";
import { useOAuthCallback } from "../hooks";
import { useServerConfig } from "@/hooks/useServerConfig";
const LoginGitlabButton = () => {
const { data: clientId } = useServerConfig("gitlab_client_id");
const discovery = useMemo(() => {
return {
authorizationEndpoint: "https://gitlab.com/oauth/authorize",
tokenEndpoint: "https://gitlab.com/oauth/token",
revocationEndpoint: "https://gitlab.com/oauth/revoke",
};
}, [clientId]);
const oauth = useOAuthCallback("gitlab");
const [request, response, promptAsync] = useAuthRequest(
{
clientId,
scopes: ["read_user"],
// redirectUri: makeRedirectUri({ scheme: appConfig.scheme }),
redirectUri: "http://localhost:8081",
},
discovery
);
useEffect(() => {
if (response?.type === "success") {
const { code } = response.params;
const verifier = request?.codeVerifier;
oauth.mutate({ code, verifier });
}
}, [response]);
return (
<Button disabled={!request} onPress={() => promptAsync()}>
Login with Gitlab
</Button>
);
};
export default LoginGitlabButton;
+3 -3
View File
@@ -1,4 +1,4 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import {
loginResultSchema,
LoginSchema,
@@ -45,8 +45,8 @@ export const useRegisterMutation = () => {
export const useOAuthCallback = (type: string) => {
return useMutation({
mutationFn: async (code: string) => {
const res = await api(`/auth/oauth/${type}/callback?code=${code}`);
mutationFn: async (params: { code: string; verifier?: string }) => {
const res = await api(`/auth/oauth/${type}/callback`, { params });
const { data } = loginResultSchema.safeParse(res);
if (!data) {
throw new Error("Invalid response!");
+2
View File
@@ -14,6 +14,7 @@ import tamaguiConfig from "@/tamagui.config";
import { useLoginMutation } from "./hooks";
import LoginGithubButton from "./components/login-github";
import { useServerConfig } from "@/hooks/useServerConfig";
import LoginGitlabButton from "./components/login-gitlab";
WebBrowser.maybeCompleteAuthSession();
@@ -109,6 +110,7 @@ export default function LoginPage() {
</Text>
{oauthList.includes("github") && <LoginGithubButton />}
{oauthList.includes("gitlab") && <LoginGitlabButton />}
</>
)}