@yct/auth
v1.0.0
Published
auth
Downloads
2
Readme
@yca/auth
Installation
npm i -S @yct/auth
Setup storage and decoder
import { Auth } from '@yct/auth';
import * as jwt from 'jsonwebtoken';
const auth = new Auth();
auth.setup({
storage: {
get: x => Promise.resolve(localStorage.getItem(x)),
set: (key, value) => Promise.resolve(localStorage.setItem(key, value)),
delete: x => Promise.resolve(localStorage.removeItem(x)),
clear: () => Promise.resolve(localStorage.clear()),
},
decoder: jwt.decode as any
});
Methods and Properties
setup(ac: AuthConfig): Promise<void>;
ready() Promise<void>;
signJwt(jwt: string): Promise<void>;
signout(): Promise<void>;
hasRoles(...roles: string[]): boolean;
onSignjwt(fn: () => Promise<void>): void;
onSignout(fn: () => Promise<void>): void;
offSignjwt(fn: () => Promise<void>): void;
offSignout(fn: () => Promise<void>): void;
enableCheckExp(interval: number, fn?: () => Promise<void>): void;
get jwt(): string;
get isAuthenticated(): boolean;
get info(): AuthInfo | null;
Interfaces and Types
export interface AuthConfig {
storage: AuthStorage;
decoder: AuthDecoder;
}
export type AuthDecoder = (jwt: string) => AuthInfo;
export interface AuthStorage {
get(key: string): Promise<any>;
set(key: string, value: any): Promise<any>;
delete(key: string): Promise<any>;
clear(): Promise<any>;
}
export interface AuthInfo {
_id: string;
roles: string[];
username: string;
providers: Array<{
name: string;
openid: string;
}>;
}