Last active
October 22, 2020 05:44
-
-
Save dabit3/1b9f5b72096a06d83a14b8e7d1085fcc to your computer and use it in GitHub Desktop.
Next.js + Amplify - helpers/checkUser.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helpers/checkUser.js | |
import { useState, useEffect } from 'react'; | |
import { Auth, Hub } from 'aws-amplify'; | |
export default function checkUser() { | |
const [user, setUser] = useState(null); | |
useEffect(() => { | |
checkUserAuth(); | |
const unsubscribe = Hub.listen('auth', () => checkUserAuth()); | |
return () => unsubscribe(); | |
}, []) | |
async function checkUserAuth() { | |
try { | |
const signedInUser = await Auth.currentAuthenticatedUser(); | |
setUser(signedInUser); | |
} catch (err) { setUser(null); }; | |
} | |
return user; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment