-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth.ts
57 lines (50 loc) · 1.48 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {
makeRedirectUri,
AuthRequest,
AuthRequestPromptOptions,
AuthSessionResult,
} from "expo-auth-session";
import { useEffect, useState, useCallback } from "react";
import { CLIENT_ID } from "yep/constants";
const redirectUri = makeRedirectUri({
scheme: __DEV__ ? "goodweebs-dev" : "goodweebs",
path: "redirect",
});
export function useAniListAuthRequest(): [
AuthRequest | null,
AuthSessionResult | null,
(options?: AuthRequestPromptOptions) => Promise<AuthSessionResult>
] {
const [request, setRequest] = useState<AuthRequest | null>(null);
const [result, setResult] = useState<AuthSessionResult | null>(null);
const AniListURL = `https://anilist.co/api/v2/oauth/authorize?client_id=${CLIENT_ID}&response_type=token`;
const promptAsync = useCallback(
async (options: AuthRequestPromptOptions = {}) => {
if (!request) {
throw new Error(
"Cannot prompt to authenticate until the request has finished loading."
);
}
const result = await request?.promptAsync(
{ authorizationEndpoint: AniListURL },
options
);
setResult(result);
return result;
},
[request?.url, AniListURL]
);
useEffect(() => {
if (AniListURL) {
const request = new AuthRequest({
usePKCE: false,
redirectUri,
scopes: [],
clientId: "",
});
request.url = AniListURL;
setRequest(request);
}
}, [AniListURL]);
return [request, result, promptAsync];
}