Authentication
Introduction
In Nylo, you can use the built-in helpers to make Authentication a breeze.
To authenticate a user, run the below command.
await Auth.authenticate();
If you'd like to add data, use the 'data' parameter.
await Auth.authenticate(data: {"token_id": "ey2sdm..."});
To retrieve the authenticated user's data, run the below.
Map user = await Auth.data();
print(user); // {token_id: ey2sdm...}
Let's imagine the below scenario.
- A user registers using an email and password.
- After registering, you create the user a session token.
- We now want to store the session token on the user's device for future use.
_login(String email, String password) async {
// 1 - Example register via an API Service
User? user = await api<AuthApiService>((request) => request.registerUser(
email: email,
password: password
));
// 2 - Returns the users session token
print(user?.token); // ey2sdm...
// 3 - Save the user to Nylo
await Auth.authenticate(data: {"token_id": user?.token});
}
Now the User will be authenticated and the data will be stored on their device.
Adding an auth user
When a user logs in to your application, you can add them using the Auth.authenticate()
helper.
_login() async {
...
await Auth.authenticate(data: {"token_id": "ey2sdm..."});
}
Retrieve an auth user's data
If a user is logged into your app, you can retrieve the user's data by calling Auth.data()
.
_getUser() async {
dynamic userData = await Auth.data();
print(userData); // {token_id: ey2sdm...}
}
Logout an auth user
When a user logs out of your application, you can remove them using the Auth.logout()
helper.
_logout() async {
await Auth.logout();
}
Now, the user is logged out of the app and the authentication page won't show when they next visit the app.
Checking if a user is authenticated
You can check if a user is authenticated by calling the Auth.isAuthenticated()
helper.
_isAuthenticated() async {
bool isAuthenticated = await Auth.isAuthenticated();
print(isAuthenticated); // true
}
Authentication Page
Once your user is stored using the Auth.authenticate(user)
helper. You'll be able to set an 'authentication page', this will be used as the initial page the user sees when they open the app.
Go to your routes/router.dart file and use the authenticatedRoute
function.
appRouter() => nyRoutes((router) {
router.add(HomePage.path).initialRoute(); // initial route
router.add(ProfilePage.path).authenticatedRoute(); // authenticated route
});
Now, when the app boots, it will use the authenticated page instead of the default route.