1- import { GithubAuthProvider , GoogleAuthProvider , signInWithPopup } from 'firebase/auth'
1+ import { AuthProvider , OAuthProvider , signInWithPopup } from 'firebase/auth'
22import { FaGithub , FaGoogle } from 'react-icons/fa'
33import ReactModal from 'react-modal'
44import toast from 'react-simple-toasts'
5+ import { useAuth } from 'src/stores/user'
56import { auth , githubProvider , googleProvider } from '../api/Config'
67
78type AuthModalProps = {
@@ -10,33 +11,30 @@ type AuthModalProps = {
1011}
1112
1213export const AuthModal = ( { showAuth, setShowAuth } : AuthModalProps ) => {
13- const signInWithGoogle = ( ) => {
14- signInWithPopup ( auth , googleProvider )
15- . then ( ( result ) => {
16- const credential = GoogleAuthProvider . credentialFromResult ( result )
17- // TODO save this token in user settings maybe!
18- const token = credential . accessToken
19- // The signed-in user info.
20- const user = result . user
21- console . log ( user )
22- } )
23- . catch ( ( error ) => {
24- toast ( "We couldn't login to you Google account!!" , { theme : 'failure' } )
25- } )
26- }
14+ const { initState } = useAuth ( )
2715
28- const signInWithGithub = ( ) => {
29- signInWithPopup ( auth , githubProvider )
16+ const signIn = ( provider : AuthProvider , providerName : string ) => {
17+ signInWithPopup ( auth , provider )
3018 . then ( ( result ) => {
31- const credential = GithubAuthProvider . credentialFromResult ( result )
32- // TODO save this token in user settings maybe!
33- const token = credential . accessToken
34- // The signed-in user info.
35- const user = result . user
36- console . log ( user )
19+ const credential = OAuthProvider . credentialFromResult ( result )
20+ const accessToken = credential ?. accessToken
21+ const email = result . user . displayName
22+ const name = result . user . displayName
23+ const imageURL = result . user . photoURL
24+ if ( accessToken && name && email && imageURL ) {
25+ initState ( {
26+ accessToken : accessToken ,
27+ user : {
28+ name : name ,
29+ email : email ,
30+ imageURL : imageURL ,
31+ } ,
32+ } )
33+ }
3734 } )
3835 . catch ( ( error ) => {
39- toast ( "We couldn't login to you Github account!!" , { theme : 'failure' } )
36+ console . log ( error )
37+ toast ( `We couldn't login to your ${ providerName } account!!` , { theme : 'failure' } )
4038 } )
4139 }
4240
@@ -66,14 +64,14 @@ export const AuthModal = ({ showAuth, setShowAuth }: AuthModalProps) => {
6664 </ p >
6765 </ div >
6866 < div >
69- < button className = "extraTextWithIconBtn" onClick = { signInWithGithub } >
67+ < button className = "extraTextWithIconBtn" onClick = { ( ) => signIn ( githubProvider , 'Github' ) } >
7068 < FaGithub />
7169 Sign in with Github
7270 </ button >
7371 < button
7472 className = "extraTextWithIconBtn"
7573 style = { { marginLeft : 10 } }
76- onClick = { signInWithGoogle } >
74+ onClick = { ( ) => signIn ( googleProvider , 'Google' ) } >
7775 < FaGoogle />
7876 Sign in with Google
7977 </ button >
0 commit comments