silent sign in using oidc client with identity server 4

Discussão em 'Angular' iniciado por user5049376, Novembro 20, 2024.

  1. user5049376

    user5049376 Guest

    I'm trying to implement silent login in oidc-client to use with Angular 2

    How can use oidc client to silently check if user is already logged in (idsvr4) and display the login details.

    the following code works, but i need to refresh the page

    idsvr 4 client

    // JavaScript Client
    new Client
    {
    ClientId = "js",
    ClientName = "JavaScript Client",
    AllowedGrantTypes = GrantTypes.Implicit,
    AllowAccessTokensViaBrowser = true,


    RedirectUris = { "http://localhost:5002/callback.html" },

    PostLogoutRedirectUris = { "http://localhost:5002/index.html" },
    AllowedCorsOrigins = { "http://localhost:5002" },

    AllowedScopes =
    {
    IdentityServerConstants.StandardScopes.OpenId,
    IdentityServerConstants.StandardScopes.Profile,
    "api1",

    },
    RequireConsent=false,
    AllowOfflineAccess = true
    }


    client side code


    var config = {
    authority: "http://localhost:5000",
    client_id: "js",
    redirect_uri: "http://localhost:5002/callback.html",
    silent_redirect_uri: "http://localhost:5002/callback.html",
    response_type: "id_token token",
    scope: "openid profile api1 offline_access",
    post_logout_redirect_uri: "http://localhost:5002/index.html",



    // Number of seconds before the token expires to trigger
    // the `tokenExpiring` event
    accessTokenExpiringNotificationTime: 4,

    // Do we want to renew the access token automatically when it's
    // about to expire?
    automaticSilentRenew: false,

    // Do we want to filter OIDC protocal-specific claims from the response?
    filterProtocolClaims: false,

    // use localStorage
    userStore: new Oidc.WebStorageStateStore({ store: window.localStorage })
    };
    var mgr = new Oidc.UserManager(config);


    // You can hook a logger to the library.
    // Conveniently, the methods exposed by the logger match
    // the `console` object
    Oidc.Log.logger = console;

    // When a user logs in successfully or a token is renewed, the `userLoaded`
    // event is fired. the `addUserLoaded` method allows to register a callback to
    // that event
    mgr.events.addUserLoaded(function (loadedUser) {
    console.log("$$$$$$$$$$$$$$$$$$$$$$$ added");
    });

    // Same mechanism for when the automatic renewal of a token fails
    mgr.events.addSilentRenewError(function (error) {
    console.error('$$$$$$$$$$$$$$$$$$$$$$$ error while renewing the access token', error);
    });

    // When the automatic session management feature detects a change in
    // the user session state, the `userSignedOut` event is fired.
    mgr.events.addUserSignedOut(function () {
    alert('The user has signed out');
    });

    mgr.getUser().then(function (user) {
    if (user) {
    log("User logged in", user.profile);
    }
    else {
    log("User not logged in");
    // log("*****************************************************");
    mgr.signinSilent()
    .then(function (newUser) {
    console.log("doneeeeeeeeeeeeeeeeeeeee");
    console.log(newUser);
    console.log(newUser.profile);
    }).catch(function (e) {
    console.log("======== " + e);
    });;
    mgr.signinSilentCallback().then(function (newUser) {
    console.log("doneeeeeeeeeeeeeeeeeeeee");
    console.log(newUser);
    console.log(newUser.profile);
    }).catch(function (e) {
    console.log("&&&&&&&&&&&& "+e);
    });

    }


    });

    no user is getting returned in either methods of silentSignIn

    I want to get if user is logged in and retrieve the information as soon as the client is open.

    Or if there's a better way to do this in angular 2 then it's better.

    Continue reading...

Compartilhe esta Página