Skip to main content

Authentication

Alapa comes with a built-in authentication mechanism for web and API endpoints.

Example Usage:

src/app/account/login.ts
import { Auth } from "alapa";

Auth.attempt("username", "password");

The attempt method returns an object containing success, reason, errorMessage, and user. It accepts an optional third argument called when. The WhenCallback, as the name suggests, is a list of functions that take user as an argument and also return success, reason, errorMessage, and user. The attempt method terminates if any of the functions return success as false.

Auth.login

To log in a user, you can use:

src/app/account/login.ts
import { Auth } from "alapa";

Auth.login(req, res, user);
// OR
req.login(user);

The login method returns an authentication result containing the user, success, message, and the destination (URL) to redirect the user to; the default is the dashboard.

The login method also has an optional parameter called rememberMe, which is a boolean argument that indicates whether the login should remember the user. If rememberMe is true, the user ID, referred to as user, and the encrypted token, called token, will be saved to the cookie.

Auth.logout

To log out a user, use:

src/app/account/login.ts
import { Auth } from "alapa";

Auth.logout();

The logout method will destroy the session of the current user.