Skip to content

Commit

Permalink
✨ Authenticate with firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jh0ker committed Apr 4, 2018
1 parent df48435 commit 93fde0d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ yarn-error.log
firebase-debug.log

spotify.config.js
firebase.config.js
.firebaserc
service-account.*
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@polymer/iron-icons": "^3.0.0-pre.12",
"@polymer/paper-button": "^3.0.0-pre.12",
"@polymer/paper-item": "^3.0.0-pre.12",
"firebase": "^4.12.1",
"fit-html": "^0.5.4",
"qs": "^6.5.1",
"redux-devtools-extension": "^2.13.2",
Expand All @@ -18,7 +19,6 @@
"devDependencies": {
"chokidar": "^2.0.3",
"connect-history-api-fallback": "^1.5.0",
"firebase": "^4.12.1",
"firebase-tools": "^3.17.7",
"rollup": "^0.57.1",
"rollup-plugin-browsersync": "^0.2.6",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const frontendConfig = {
ui: false
}) : null,
].filter((plugin) => plugin !== null),
//onwarn: err => console.error(err.toString()),
onwarn: err => console.error(err.toString()),
watch: {
include: 'src/**/*'
},
Expand Down
5 changes: 4 additions & 1 deletion src/sagas/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "../util/spotify";
import {authInitFinished} from "../actions/auth";
import {ExchangeCodeResult} from "../../functions/src";
import firebase from "../util/firebase";

const OAUTH_STATE = 'POPOPO';

Expand Down Expand Up @@ -45,7 +46,9 @@ function* exchangeCode() {
refreshToken,
}: ExchangeCodeResult = yield spotifyExchangeCode(code, window.location.origin);

storeAuthData(accessToken, firebaseToken, expiresIn, refreshToken);
yield firebase.auth!().signInWithCustomToken(firebaseToken);

storeAuthData(accessToken, expiresIn, refreshToken);
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions src/util/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import firebase from '@firebase/app';
import '@firebase/auth';
import '@firebase/database';

import firebaseConfig from '../../firebase.config.js';

const app = firebase.initializeApp(firebaseConfig)!;
export default app;

export {
app as firebase,
firebase as firebaseNS,
};
8 changes: 3 additions & 5 deletions src/util/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ export const SCOPES = [
'user-library-read',
];

export function storeAuthData(accessToken: string, firebaseToken: string, expiresIn: number, refreshToken: string) {
export function storeAuthData(accessToken: string, expiresIn: number, refreshToken: string) {
localStorage[LOCAL_STORAGE_KEY] = JSON.stringify({
accessToken,
firebaseToken,
expiresAt: Date.now() + (expiresIn * 1000),
refreshToken,
} as SpotifyAuthStorage);
Expand All @@ -46,7 +45,6 @@ export class UserNotLoggedInError extends Error {

export interface SpotifyAuthStorage {
accessToken: string;
firebaseToken: string;
expiresAt: number;
refreshToken: string;
}
Expand Down Expand Up @@ -84,7 +82,7 @@ export async function requireAccessToken() {
throw new UserNotLoggedInError();
}

const { accessToken, firebaseToken, expiresAt, refreshToken }: SpotifyAuthStorage = JSON.parse(authString);
const { accessToken, expiresAt, refreshToken }: SpotifyAuthStorage = JSON.parse(authString);

if (Date.now() <= expiresAt) {
return accessToken;
Expand All @@ -97,7 +95,7 @@ export async function requireAccessToken() {
throw new UserNotLoggedInError();
}

storeAuthData(newAccessToken, firebaseToken, expiresIn, refreshToken);
storeAuthData(newAccessToken, expiresIn, refreshToken);
return newAccessToken;
}

Expand Down

0 comments on commit 93fde0d

Please sign in to comment.