Using JWT for authentication in an ionic 3 applica

2019-02-25 15:14发布

I have an ionic 3 application where I want to use the touchId feature.

The backend API is being developed in .net.

For my authentication model, I'm thinking that after a user enters a login name and password and is authenticated by the server, it sends back a JWT token.

I can store the token locally and then every time a user uses the touchId it checks if a token is stored locally, then matches that token to the server and that will allow access.

Is this how it works?

1条回答
太酷不给撩
2楼-- · 2019-02-25 15:46

you can use angular2-jwt, it works perfectly and send your token automatically with all your http requests and you can manage your token expiration very easy

you can only give the source of your access token in your config file from ionic storage, here is the config for ionic:

import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt';
import { Storage } from '@ionic/storage';

export function jwtOptionsFactory(storage) {
return {
  tokenGetter: () => {
   return storage.get('access_token');
 }
}
}


@NgModule({

imports: [
 JwtModule.forRoot({
    jwtOptionsProvider: {
    provide: JWT_OPTIONS,
    useFactory: jwtOptionsFactory,
    deps: [Storage]
  }
})
]})
查看更多
登录 后发表回答