Authentication
Alapa comes with a built-in authentication mechanism for web and API endpoints.
Example Usage:
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:
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:
import { Auth } from "alapa";
Auth.logout();
The logout
method will destroy the session of the current user.