Search code examples
reactjsdjangodjango-rest-frameworkdjango-oauthdjango-oauth-toolkit

Get User model data in drf-social-oauth2 response after login


So I am using drf-social-oauth2 for authentication and React in the frontend,

const handleGoogleLogin = (response) => {
    setLoading(true);
    axios
      .post(`${apiBaseURL}/auth/convert-token`, {
        token: response.accessToken,
        backend: "google-oauth2",
        grant_type: "convert_token",
        client_id: drfClientId,
        client_secret: drfClientSecret,
      })
      .then((res) => {
        const { access_token, refresh_token } = res.data;
        const cookies = new Cookies();
        cookies.remove("google_access_token");
        cookies.remove("google_refresh_token");
        cookies.set("google_access_token", access_token, {path: "/", maxAge: 24*60*60});
        cookies.set("google_refresh_token", refresh_token, {path: "/", maxAge: 24*60*60});
        setLoading(false);
        console.log(res);
        console.log(res.data);
        // window.location.href = `${appBaseURL}/`;
      })
      .catch((err) => {
        setLoading(false);
        createErrorNotification();
      });
  };

Here the login is successful and everything works fine, however the response I get is just

{access_token: 't3LqiUvz0x4HBWGDsSyLP7fsyKejJf', expires_in: 63583.018772, scope: 'read write', refresh_token: 'GEkGz8teGATlYY0pQ1zuntN8ODfFT4', token_type: 'Bearer'}

What can I do to have all the user info like his model fields in the response? Couldn't find anything related to this anywhere


Solution

  • You can set on your settings like this to get data from google.

    SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = [
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
    ]
    

    and that will give you data of user like, "email, first_name, last_name, usern_name"