Skip to content

Commit

Permalink
Store Spotify credentials and use to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
Advayp committed Jan 31, 2025
1 parent 056a195 commit 6bc37fe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
12 changes: 11 additions & 1 deletion frontend/app/register/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,26 @@ import { register } from '@/services/auth/register';
import { Button } from '@/components/Button';
import { Spinner } from '@/components/Spinner';
import { useRouter } from 'expo-router';
import {
getSpotifyCredentials,

Check warning on line 11 in frontend/app/register/index.tsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'getSpotifyCredentials' is defined but never used
validateSpotifyCredentials,
} from '@/utils/spotify';

const Index = () => {
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState<string>();
const [isLoading, setIsLoading] = useState(false);


const router = useRouter();
const registerUser = async () => {
const isSpotifyValid = await validateSpotifyCredentials();

if (!isSpotifyValid) {
setError('Please authorize Spotify before continuing');
return;
}

setError(undefined);
setIsLoading(true);
const [_, error] = await register(username, password);

Check warning on line 32 in frontend/app/register/index.tsx

View workflow job for this annotation

GitHub Actions / lint-frontend

'_' is assigned a value but never used
Expand Down
34 changes: 34 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"react-native-screens": "~4.4.0",
"react-native-web": "~0.19.13",
"react-native-webview": "13.12.5",
"expo-secure-store": "~14.0.1"
"expo-secure-store": "~14.0.1",
"@react-native-async-storage/async-storage": "1.23.1"
},
"devDependencies": {
"@babel/core": "^7.25.2",
Expand Down
43 changes: 43 additions & 0 deletions frontend/utils/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { AccessTokenResponse } from '../types';
import { makeRedirectUri } from 'expo-auth-session';
import AsyncStorage from '@react-native-async-storage/async-storage';

const ACCESS_TOKEN_KEY = 'spotify-access-token';
const EXPIRATION_KEY = 'spotify-expiration';
const REFRESH_TOKEN_KEY = 'spotify-refresh-token';

export const getClientId = () => {
return process.env.EXPO_PUBLIC_SPOTIFY_CLIENT_ID;
Expand Down Expand Up @@ -27,10 +32,48 @@ export const fetchAccessToken = async (code: string) => {
},
});
const json = (await response.json()) as AccessTokenResponse;
saveSpotifyCredentials(
json.access_token,
json.expires_in,
json.refresh_token
);
console.log(json);
return {
accessToken: json.access_token,
expiration: json.expires_in,
refreshToken: json.refresh_token,
};
};

export const saveSpotifyCredentials = async (
accessToken: string,
expiration: number,
refreshToken: string
) => {
await AsyncStorage.setItem(ACCESS_TOKEN_KEY, accessToken);
await AsyncStorage.setItem(
EXPIRATION_KEY,
(Date.now() + expiration).toString()
);
await AsyncStorage.setItem(REFRESH_TOKEN_KEY, refreshToken);
};

export const getSpotifyCredentials = async () => {
const storedExpiration = await AsyncStorage.getItem(EXPIRATION_KEY);
return {
accessToken: await AsyncStorage.getItem(ACCESS_TOKEN_KEY),
expiration: storedExpiration ? parseInt(storedExpiration) : null,
refreshToken: await AsyncStorage.getItem(REFRESH_TOKEN_KEY),
};
};

export const validateSpotifyCredentials = async () => {
const { accessToken, expiration, refreshToken } =
await getSpotifyCredentials();

if (!expiration || expiration > Date.now()) {
return false;
}

return accessToken && refreshToken;
};

0 comments on commit 6bc37fe

Please sign in to comment.