DEV Community

loading...

Firebase github authentication

thefierycoder profile image Rishi Purwar ・1 min read

I want to know why these values are not logging to the console. I want to access the result object and then set the user using useState hook. Whenever I click on the sign-in button. It doesn't log anything to the console. Just login the user but I want to access the username property which is stored here (result.additionalUserInfo.username) Please help me with this issue
console.log(result.additionalUserInfo.username);
console.log(result.additionalUserInfo.profile);

This is my code

```import React, { useState,useContext } from 'react'
import { createContext } from 'react';
import { useEffect } from 'react';
import {auth} from '../config/fbConfig'

const AuthContext = createContext();

export const useAuth = () => {
return useContext(AuthContext);
}

export const AuthProvider = ({children}) => {
const [currentUser, setCurrentUser] = useState();
const [loading, setLoading] = useState(true);

const provider = new auth.GithubAuthProvider();
// provider.addScope('user');
const githubSignIn = () => {
    // return auth().signInWithRedirect(provider);
    auth().signInWithRedirect(provider)
    .then(function(result) {
    console.log(result.additionalUserInfo.username);
    console.log(result.additionalUserInfo.profile);
    })
    .catch(function(error) {
        // Some error occurred.
        console.log(error)
    });
}

const githubSignOut = () => {
    return auth().signOut();
}

useEffect(() => {
    const unsubscribe = auth().onAuthStateChanged(user => {
        // setCurrentUser(user);
        setLoading(false);
    })
    return unsubscribe
}, [])

const value = {
    currentUser,
    githubSignIn,
    githubSignOut
}

return (
    <AuthContext.Provider value={value}>
        {!loading && children}
    </AuthContext.Provider>
)
Enter fullscreen mode Exit fullscreen mode

}```

Discussion (0)

pic
Editor guide