Skip to content

Commit

Permalink
Merge pull request #51 from anaswaratrajan/token
Browse files Browse the repository at this point in the history
Refresh token after expiry
  • Loading branch information
anastr0 authored Jan 30, 2021
2 parents 43a7c5f + 45c04b2 commit 6e46252
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/utils/privateAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
import React, { useEffect } from "react";
import { useSelector } from "react-redux";
import { useSelector, useDispatch } from "react-redux";
import { NextPage } from "next";
import Link from "next/link";
import Router from "next/router";
import { RootState } from "../store/store";
import { firebase } from "./firebase"
import { login } from "../store/auth/action";

// private route for logged in users only
const privateAuthWrapper = (Component: NextPage) => {
const Auth = (props: any) => {
const isLoggedIn = useSelector(
(state: RootState) => state.authReducer.isLoggedIn
);

const dispatch = useDispatch();
useEffect(() => {
if (!isLoggedIn) {
Router.push("/");
}
}, [isLoggedIn]);

useEffect(() => {
/*
an observer for changes to the `signed-in user's ID token`,
which includes sign-in, sign-out, and token refresh events.
https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onidtokenchanged
*/
firebase.auth().onIdTokenChanged((user) => {
if (user) {
user.getIdToken(true)
.then((token) => {
// console.log(token)
dispatch(
login({ displayName: user.displayName, username: user.email, token })
);
})
}
})
})

// If user is not logged in, return login component
if (isLoggedIn === false) {
return (
Expand Down

1 comment on commit 6e46252

@vercel
Copy link

@vercel vercel bot commented on 6e46252 Jan 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.