Skip to content

Passport

When a user accesses the backend API, the system verifies the JWT token. If the verification passes, a Passport is created. This passport contains information such as the current user and authentication info

a-user: IPassport

The module a-user provides the interface IPassport, which defines the basic fields of the Passport

typescript
export interface IPassport {
  user?: IUser;
  auth?: IAuth;
  roles?: IRole[];
}

a-user: bean.passport

The module a-user provides the global Bean bean.passport, providing a common calling convention for business logic

typescript
// get the current passport
const passport = this.bean.passport.current;
// mock signin for test
this.bean.passport.signinMock();
  • bean.passport Method List
NameDescription
isAuthenticatedChecks if the current user is authenticated (not anonymous)
isActivatedChecks if the current user is activated
isAdminChecks if the current user is an admin user
setCurrentSets the current Passport
getCurrentGets the current Passport
getCurrentUserGets the current user
getCurrentAuthGets the current authentication
getCurrentRolesGets the current role
signinCompletes the login logic for the passport and returns a JWT token
signoutLogs out
signinSystemLogs in as a system user
signinMockMock login for testing
signinWithAnonymousLogs in as an anonymous user
kickOutKicks out the user
checkAuthTokenValidates the JWT token; creates a Passport if validation is successful
refreshAuthTokenRefreshes the JWT token
createTempAuthTokenGenerates a temporary JWT token with a short expiration time, typically used in URL query parameters

home-user adapter: ServicePassportAdapter

The home-user module provides the adapter ServicePassportAdapter, allowing us to customize the passport operation logic. The business logic calls bean.passport, which in turn calls ServicePassportAdapter, thus achieving a perfect combination of out-of-the-box and flexible customization

src/suite/a-home/modules/home-user/src/service/passportAdapter.ts

NameDescription
isAdminChecks if the current user is an admin user
setCurrentSets the current Passport
serializeSerializes the Passport into a payload, storing it in a JWT token
deserializeDeserializes the Passport from the payload

Get the current Passport

diff
class ControllerStudent {
  @Web.get('test')
  test() {
+   const passport = this.bean.passport.current;
    console.log(passport);
  }
}

Or:

diff
class ControllerStudent {
  @Web.get('test')
  test() {
+   const passport = this.ctx.passport;
    console.log(passport);
  }
}

a-user: Passport Decorators

The a-user module provides a set of Passport decorators for verifying permissions for the current user

For many small to medium-sized projects, this set of Passport decorators is sufficient to meet their needs. For more advanced permission verification, you can develop your own Guard. See:

NameDescription
publicWhether to allow anonymous users to access the API
activatedWhether to allow inactive users to access the API
userNameChecks the current username
roleNameChecks the current user's role name
adminChecks if the current user's role name is admin

This only lists common syntax. For detailed information, see: Built-in Guard

typescript
import { Passport } from 'vona-module-a-user';

@Passport.public()
@Passport.activated(false)
@Passport.userName({ name: 'admin' })
@Passport.roleName({ name: 'admin' })
@Passport.admin()

home-user: Passport API

The home-user module provides a set of Passport APIs out of the box, and custom business logic can be extended on top of this

src/suite/a-home/modules/home-user/src/controller/passport.ts

NameDescription
currentGet the current Passport
logoutLog out
registerRegister a new user
loginLog in
loginOauthOAuth authentication
associateAssociate authentication
migrateMigrate authentication
refreshAuthTokenRefresh JWT token
createPassportJwtFromOauthCodeGenerate a JWT token using the code. This code is returned by OAuth authentication
createTempAuthTokenGenerates a temporary JWT token with a short expiration time, typically used in URL query parameters

Released under the MIT License.