-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuseSpotifyAuthentication.ts
83 lines (72 loc) · 2.2 KB
/
useSpotifyAuthentication.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { useEffect, useState } from 'react';
import { Platform, AsyncStorage } from 'react-native';
import * as AuthSession from 'expo-auth-session';
import { SPOTIFY_CLIENT_ID, spotifyApi } from './spotify';
const SCOPE = [
'playlist-modify-public',
'playlist-modify-private',
'user-read-private',
'ugc-image-upload',
'user-follow-read',
'user-library-read',
'user-top-read',
'user-read-recently-played',
];
const redirectUrl = AuthSession.getRedirectUrl();
const persistAuthState = (token: string, expiresIn: number) =>
AsyncStorage.setItem('authState', JSON.stringify({ token, expiresIn }));
const getLocalAuthState = async () => {
const jsonString = await AsyncStorage.getItem('authState');
try {
return jsonString ? JSON.parse(jsonString) : null;
} catch {
return null;
}
};
export const useSpotifyAuthentication = () => {
const [token, setToken] = useState('');
const [expires, setExpires] = useState<number | undefined>(undefined);
const [, result, promptAsync] = AuthSession.useAuthRequest(
{
clientId: SPOTIFY_CLIENT_ID,
redirectUri: redirectUrl,
scopes: SCOPE,
usePKCE: false,
responseType: AuthSession.ResponseType.Token,
},
{
authorizationEndpoint: 'https://accounts.spotify.com/authorize',
tokenEndpoint: '', // typed as required, but not used with implicit grant
},
);
useEffect(() => {
if (result?.type === 'success') {
const { access_token: token, expires_in: expiresIn } = result.params;
const expires = new Date().getTime() + Number(expiresIn) * 1000;
setToken(token);
setExpires(expires);
persistAuthState(token, expires);
}
}, [result?.type]);
useEffect(() => {
getLocalAuthState().then((authState) => {
if (authState) {
const timeNow = new Date().getTime();
if (authState.expiresIn > timeNow) {
setToken(authState.token);
setExpires(authState.expiresIn);
}
}
});
}, []);
useEffect(() => {
if (token && spotifyApi) {
spotifyApi.setAccessToken(token);
}
}, [token, spotifyApi]);
return {
token,
expires,
handleLogin: () => promptAsync({ useProxy: Platform.OS !== 'web' }),
};
};