mirror of
https://github.com/khairul169/vaulterm.git
synced 2026-09-15 17:03:30 +07:00
feat: add gitlab oauth, cleanup code
This commit is contained in:
@@ -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;
|
||||
@@ -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!");
|
||||
|
||||
@@ -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 />}
|
||||
</>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user