nocobase/packages/core/auth/src/auth.ts
YANG QIA 06f11a2d08
refactor(auth): move auth client from core to the plugin & refactor auth client api (#3215)
* refactor(auth): auth client api

* fix: build

* fix: dependencies

* fix: fix T-2777

* fix: fix T-2776

* chore: update type

* fix: build

* fix: allowSignUp

* fix: file name

* fix: file name

* refactor: client api

* fix: build

* chore: update name

* fix: tsx must be loaded with --import instead of --loader

* fix: type

* fix: type

* fix: type

* fix: type

* fix: bug

* chore: improve wording

* fix: test

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-12-21 20:19:25 +08:00

46 lines
1.2 KiB
TypeScript

import { Context } from '@nocobase/actions';
import { Model } from '@nocobase/database';
import { Authenticator } from './auth-manager';
export type AuthConfig = {
authenticator: Authenticator;
options: {
[key: string]: any;
};
ctx: Context;
};
export type AuthExtend<T extends Auth> = new (config: AuthConfig) => T;
interface IAuth {
user: Model;
// Check the authenticaiton status and return the current user.
check(): Promise<Model>;
signIn(): Promise<any>;
signUp(): Promise<any>;
signOut(): Promise<any>;
}
export abstract class Auth implements IAuth {
abstract user: Model;
protected authenticator: Authenticator;
protected options: {
[key: string]: any;
};
protected ctx: Context;
constructor(config: AuthConfig) {
const { authenticator, options, ctx } = config;
this.authenticator = authenticator;
this.options = options;
this.ctx = ctx;
}
// The abstract methods are required to be implemented by all authentications.
abstract check(): Promise<Model>;
// The following methods are mainly designed for user authentications.
async signIn(): Promise<any> {}
async signUp(): Promise<any> {}
async signOut(): Promise<any> {}
}