Skip to content

Commit

Permalink
Setup initial skeleton for Spotify Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Advayp committed Jan 25, 2025
1 parent 5e41a22 commit 2c5f449
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
9 changes: 1 addition & 8 deletions frontend/app/(tabs)/explore.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import React from 'react';
import { Text, SafeAreaView, Button } from 'react-native';
import { TEST } from 'react-native-dotenv';
import { Text, SafeAreaView } from 'react-native';

export default function TabTwoScreen() {
return (
<SafeAreaView>
<Text>Tab Two</Text>
<Button
title="asdf"
onPress={() => {
console.log(TEST);
}}
/>
</SafeAreaView>
);
}
40 changes: 40 additions & 0 deletions frontend/package-lock.json

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

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@react-navigation/bottom-tabs": "^7.2.0",
"@react-navigation/native": "^7.0.14",
"expo": "~52.0.25",
"expo-auth-session": "^6.0.2",
"expo-blur": "~14.0.2",
"expo-constants": "~17.0.4",
"expo-font": "~13.0.3",
Expand Down
10 changes: 10 additions & 0 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {},
"extends": "expo/tsconfig.base",
"include": [
"**/*.ts",
"**/*.tsx",
".expo/types/**/*.ts",
"expo-env.d.ts"
]
}
5 changes: 5 additions & 0 deletions frontend/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface AccessTokenResponse {
access_token: string;
refresh_token: string;
expires_in: number;
}
42 changes: 42 additions & 0 deletions frontend/utils/spotify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { AccessTokenResponse } from '../types';
import { makeRedirectUri } from 'expo-auth-session';
import { SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET } from 'react-native-dotenv';

export const getClientId = () => {
return SPOTIFY_CLIENT_ID;
};

const getClientSecret = () => {
return SPOTIFY_CLIENT_SECRET;
};

const generateAccessTokenParams = (code: string) => {
const params = new URLSearchParams();

params.append('grant_type', 'authorization_code');
params.append('code', code);
params.append('redirect_uri', makeRedirectUri());

return params.toString();
};

export const fetchAccessToken = async (code: string) => {
const response = await fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
body: generateAccessTokenParams(code),
headers: {
'content-type': 'application/x-www-form-urlencoded',
Authorization: 'Basic ' + btoa(getClientId() + ':' + getClientSecret()),
},
});

const json = (await response.json()) as AccessTokenResponse;

console.log(json);

return {
accessToken: json.access_token,
expiration: json.expires_in,
refreshToken: json.refresh_token,
};
};

0 comments on commit 2c5f449

Please sign in to comment.