DEV Community

Cover image for Federated Auth & O Auth 2.0
Srushti Kulkarni
Srushti Kulkarni

Posted on

Federated Auth & O Auth 2.0

Hello All! From 1 week I'm working on federated auth(Google Sign in) & I've encountered with lots of errors.

So lets understand,

*## what is Federated Auth?
*

Basically its a system that allows user to login across multiple application or domain
 using single set of credentials which are managed by trusted identity provider(Google, Apple) without having to create a new account for each one.

  • That means when you do sign in with google or apple then you are using federated auth.
  • Now lets understand it with the help of below dig.

This is the flow of when you do sign in with Google.
So basically,

  1. when you do sign in with google it redirect to google's login page
  2. when user login with credentials, google verifies the identity and sends a access token / id_token. 3.The application verifies the token i.e is token expired or not & if not 4.Backend uses a token's info i.e Google account info like profile photo, username, emailId etc.

and when the authentication done, you've to do one thing i.e you've to set the isLoggedIn : true , if our backedn verifies that data it should redirect to desired page.

// Assume this is the function called after checking the authentication status
function handleAuthStatus(authStatus) {
    // Check if the user is successfully authenticated
    if (authStatus.logged === true) {
        console.log("Authentication successful.Redirecting to dashboard");

        // Redirect the user to the main application page
        window.location.href = '/dashboard'; 

    } else {
       // Redirect the user to the main login page again
        window.location.href = '/login';
    }
}

// Example usage with a dummy check 

const userSession = {
    logged: true, // This flag would be determined by a valid token/session
    username: "user123"
};

handleAuthStatus(userSession);
Enter fullscreen mode Exit fullscreen mode

OAuth 2.0

  • In simple language OAuth is , one application access data from another application with user concent.
  • Simple eg. , Allowing canva to access insta photos/media.
  1. Suppose you are on canva and you click: Connect to instagram.
  2. Canva Redirect you to instagram login page.
  3. Canva wants to access your media so instagram asks you to allow or not allowed.
  4. After allowing insta issues an access token.
  5. Canva use that token to fetch your photos from instagram.

this is the process of OAuth. These 2 are almost same but little difference is that in federated auth it trusts on identity provider and verifies the access token and in OAuth it takes concent from user.

Top comments (0)