@dvs-angular/auth-http-client
v0.0.7
Published
Requirements:
Downloads
10
Readme
Angular Components - AuthHttpClient
Requirements:
'@dvs-angular/storage'
and itsStorage
provided
Usage
Start
in
app.module
import { AuthHttpClientModule } from '@dvs-angular/auth-http-client';
add it into app imports
Set token
- if you get your token (e.g. after login) use
TokenService
to store it
`import { TokenService, Token } from '@dvs-angular/auth-http-client';`
...
tokenService.setToken(new Token('yourToken'))
How to use
AuthHttpClient
extends HttpClient
so you can use it as regular HttpClient
AuthHttpClient
offers you subscription to denied responses only injectAuthHttpClient
and subscribe toonAccessDenied
subject
Refresh token
AuthHttpClient
supports refresh token, to use it, you have to provide REFRESH_TOKEN_API_URL
e.g.
import { REFRESH_TOKEN_API_URL } from '@dvs-angular/auth-http-client';
...
{
provide: REFRESH_TOKEN_API_URL,
useValue: environment.apiUrl + 'token/refresh'
}
and when you set Token
you must pass second argument with refreshToken
new Token('yourToken', 'yourRefreshToken')
Config
You can override some values by providing them in your module
TOKEN_STORAGE_NAME
(default:'token'
)REFRESH_TOKEN_STORAGE_NAME
(default:'refreshToken'
)
e.g.
import { TOKEN_STORAGE_NAME } from '@dvs-angular/auth-http-client';
...
{
provide: TOKEN_STORAGE_NAME,
useValue: 'myTokenStorageSuperName'
}
API requirements
method: POST
body:
{
refresh_token: 'yourRefreshToken'
}
- response:
{
token: 'newToken',
refresh_token: 'refreshToken'
}
When everything is set, AuthHttpClient
after 401
response tries to refresh token and repeat request, so you have to do... nothing :)
Public Methods
TokenService
setToken(token: Token): void;
sets and stores tokens, remember to use Token
model
getTokenString(): string;
returns raw stored token
canRefreshToken(): boolean;
tells if refreshing is possible
clearToken(): void;
unsets and removes from storage tokens
getPrefixedToken(): string;
returns token as it's passed in header
getTokenHeaderName(): string;
returns name of header which passes the token
setRefreshTokenApiUrl(url: string): void;
allows you to set token api url config later than in provide
e.g. from async config
it's recommended to set this in APP_INITIALIZER factory
refresh(): Observable<Token>;
refreshes token