damienbod / angular-auth-oidc-client

npm package for OpenID Connect, OAuth Code Flow with PKCE, Refresh tokens, Implicit Flow
https://www.npmjs.com/package/angular-auth-oidc-client
MIT License
1.13k stars 429 forks source link

[Question]: Implementing Custom Storage in standalone api #1977

Closed salamichele closed 1 week ago

salamichele commented 2 months ago

Can someone provide any example of setting up custom storage using standalone api ? My goal is to store access token in local storage instead of session storage. Docs does not provide any references

jaredhan418 commented 1 month ago

I met the same question, after looked into source code. I found a way to use localstorage. below is provideAuth source code, use can see AbstractSecurityStorage using session storage

export function _provideAuth(passedConfig) {
    return [
        // Make the PASSED_CONFIG available through injection
        { provide: PASSED_CONFIG, useValue: passedConfig },
        // Create the loader: Either the one getting passed or a static one
        passedConfig?.loader || {
            provide: StsConfigLoader,
            useFactory: createStaticLoader,
            deps: [PASSED_CONFIG],
        },
        {
            provide: AbstractSecurityStorage,
            useClass: DefaultSessionStorageService,
        },
        { provide: AbstractLoggerService, useClass: ConsoleLoggerService },
    ];
}

So, for overriding it, add this line below

//app.config.ts

providers: [
provideAuth(authConfig),
  // this is for overriding default config, must after procideAuth
  { provide: AbstractSecurityStorage, useClass: DefaultLocalStorageService }
]
timdeschryver commented 1 month ago

This is documented at https://nice-hill-002425310.azurestaticapps.net/docs/documentation/configuration#using-localstorage-instead-of-default-sessionstorage (for Modules).

As @jaredhan418 , you can provide your own implementation or use the local storage implementation:

{ provide: AbstractSecurityStorage, useClass: DefaultLocalStorageService }