Authentication
Masonite makes authentication really simply.

Authentication Scaffold Command

Masonite comes with a command to scaffold out a basic authentication system. You may use this as a great starting point for adding authentication to your application. This command will create controllers, views, and mailables for you.
If you would like to implement your own authentication from scratch you can skip to the sections below.
First run the command to add the news files:
1
python craft auth
Copied!
Then add the authentication routes to your routes file:
1
from masonite.authentication import Auth
2
3
ROUTES = [
4
# routes
5
]
6
7
ROUTES += Auth.routes()
Copied!
You may then go to the /login or /register route to implement your authentication.

Configuration

The configuration for Masonite's authentication is quite simple:
1
from app.User import User
2
3
GUARDS = {
4
"default": "web",
5
"web": {"model": User},
6
"password_reset_table": "password_resets",
7
"password_reset_expiration": 1440, # in minutes. 24 hours. None if disabled
8
}
Copied!
The default key here is the guard to use for authentication. The web dictionary is the configuration for the web guard.

Login Attempts

You can attempt a login by using the Auth class and using the attempt method:
1
from masonite.authentication import Auth
2
from masonite.request import Request
3
4
def login(self, auth: Auth, request: Request):
5
user = auth.attempt(request.input('email'), request.input("password"))
Copied!
If the attempt succeeds, the user will now be authenticated and the result of the attempt will be the authenticated model.
If the attempt fails then the result will be None.
If you know the primary key of the model, you can attempt by the id:
1
from masonite.authentication import Auth
2
from masonite.request import Request
3
4
def login(self, auth: Auth, request: Request):
5
user = auth.attempt_by_id(1)
Copied!
You can logout the current user:
1
from masonite.authentication import Auth
2
from masonite.request import Request
3
4
def logout(self, auth: Auth):
5
user = auth.logout()
Copied!

User

You can get the current authenticated user:
1
from masonite.authentication import Auth
2
from masonite.request import Request
3
4
def login(self, auth: Auth, request: Request):
5
user = auth.user() #== <app.User.User>
Copied!
If the user is not authenticated, this will return None.

Routes

You can register several routes quickly using the auth class:
1
from masonite.authentication import Auth
2
3
ROUTES = [
4
#..
5
]
6
7
ROUTES += Auth.routes()
Copied!
This will register the following routes:
URI
Description
GET /login
Displays a login form for the user
POST /login
Attempts a login for the user
GET /home
A home page for the user after a login attempt succeeds
GET /register
Displays a registration form for the user
POST /register
Saved the posted information and creates a new user
GET /password_reset
Displays a password reset form
POST /password_reset
Attempts to reset the users password
GET /change_password
Displays a form to request a new password
POST /change_password
Requests a new password

Guards

Guards are encapsulated logic for logging in, registering and fetching users. The web guard uses a cookie driver which sets a token cookie which is used later to fetch the user.
You can switch the guard on the fly to attempt authentication on different guards:
1
from masonite.authentication import Auth
2
from masonite.request import Request
3
4
def login(self, auth: Auth, request: Request):
5
user = auth.guard("custom").attempt(request.input('email'), request.input("password")) #== <app.User.User>
Copied!