netlify_auth

Last updated:

0 purchases

netlify_auth Image
netlify_auth Images
Add to Cart

Description:

netlify auth

gotrue-dart library #
This is a Dart client library for the GoTrue API.
It lets you create and authenticate users and is a building block for constructing
the UI for signups, password recovery, login and logout.
Play around the methods via the demo site.
Installation #
To use this plugin, add netlify_auth as a dependency in your pubspec.yaml file.
$ flutter pub add netlify_auth
copied to clipboard
Usage #
import 'package:netlify_auth/netlify_auth.dart';

// Instantiate the GoTrue auth client with a GoTrueInit configuration

GoTrue auth = GoTrue(
GoTrueInit(
APIUrl: 'https://brave-colden-1959c1.netlify.app/.netlify/identity',
setCookie: true,
),
);
copied to clipboard
GoTrue configuration #
APIUrl: The absolute path of the GoTrue endpoint. To find the APIUrl, go to Identity page of your Netlify site dashboard.
audience(optional): audience is one of the pre-defined JWT payload claims. It's an optional attribute which is set to be empty by default. If you were hosting your own identity service and wanted to support multitenancy, you would need audience to separate the users.
setCookie(optional): set to be false by default. If you wish to implement the remember me functionality, set the value to be true.
Authentication examples #
Create a new user #
Create a new user with the specified email and password
auth.signup(email, password);
copied to clipboard
Example usage:
auth
.signup(email, password)
.then(response => debugPrint("Confirmation email sent"))
.catchError(error => debugPrint("It's an error"));
copied to clipboard
Example response object:
User (
id: 'example-id',
aud: '',
role: '',
email: '[email protected]',
confirmation_sent_at: '2018-04-27T22:36:59.636416916Z',
app_metadata: { provider: 'email' },
user_metadata: null,
created_at: '2018-04-27T22:36:59.632133283Z',
updated_at: '2018-04-27T22:37:00.061039863Z'
)
copied to clipboard
Also, make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.

If the registration preferences is set to be Invite only, you'll get an error message like this:
{code: 403, msg: 'Signups not allowed for this instance'}
Confirm a new user signup #
This function confirms a user sign up via a unique confirmation token
auth.confirm(confirmationLink);
copied to clipboard
When a new user signed up, a confirmation email will be sent to the user if Autoconfirm isn't turned on under the identity settings.
In the email, there's a link that says "Confirm your email address".
When a user clicks on the link, it'll be redirected to the site with a fragment identifier #confirmation_token=Iyo9xHvsGVbW-9A9v4sDmQ in the URL.
If you wish to confirm a user use the auth.confirm(confirmationLink).
Example usage:
auth
.confirm(confirmationLink)
.then((response) {
debugPrint("Confirmation email sent"));
})
.catchError((e) {
debugPrint(e.toString());
});
copied to clipboard
Check confirmed user #
This function returns a boolean when call of the users status
user.isConfirmed
copied to clipboard
Example usage:
if (user.isConfirmed) {
Navigator.push(context,
MaterialPageRoute(
builder: (context) => const Dashboard()));
} else {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const ConfirmationPage()));
}
copied to clipboard
Login a user #
Handles user login via the specified email and password
auth.login(email, password)
Example usage:
auth
.login(email.value, password.value)
.then(response => showMessage("Success! Response: " , form))
.catchError(error => showMessage("Failed"));
copied to clipboard
Example response object:
User(
api: {
"apiURL": "https://example.netlify.com/.netlify/identity",
"_sameOrigin": true,
"defaultHeaders": {}
},
url: "https://example.netlify.com/.netlify/identity",
toke: Token(
access_token: "example-jwt-token",
token_type: "bearer",
expires_in: 3600,
refresh_token: "example-refresh_token",
expires_at: 1526062471000
),
id: "example-id",
aud: "",
role: "",
email: "[email protected]",
confirmed_at: "2018-05-04T23:57:17Z",
app_metadata: {
"provider": "email"
},
user_metadata: {},
created_at: "2018-05-04T23:57:17Z",
updated_at: "2018-05-04T23:57:17Z"

)
copied to clipboard
Request password recover email #
This function sends a request to GoTrue API and triggers a password recovery email to the specified email address.
Similar to confirmation_token, the recovery_token is baked in the link of the email. You can also copy the link location from the email and run curl -I in the command line to grab the token.
auth.requestPasswordRecovery(email)
Example usage:
auth
.requestPasswordRecovery(email)
.then(response => debugPrint("Recovery email sent"))
.catchError(error => debugPrint("Error sending recovery mail"));
copied to clipboard
Example response object:
{}
Recover a user account #
This function recovers a user account via a recovery token
auth.recover(recoveryLink)
Example usage:
auth
.recover(recoveryLink)
.then(response =>
debugPrint("Logged in as %s")
)
.catchError(error => debugPrint("Failed to verify recovery token"));
copied to clipboard
Example response object:
User(
api: {
"apiURL": "https://example.netlify.com/.netlify/identity",
"_sameOrigin": true,
"defaultHeaders": {}
},
url: "https://example.netlify.com/.netlify/identity",
toke: Token(
access_token: "example-jwt-token",
token_type: "bearer",
expires_in: 3600,
refresh_token: "example-refresh_token",
expires_at: 1526062471000
),
id: "example-id",
aud: "",
role: "",
email: "[email protected]",
confirmed_at: "2018-05-04T23:57:17Z",
app_metadata: {
"provider": "email"
},
user_metadata: {},
created_at: "2018-05-04T23:57:17Z",
updated_at: "2018-05-04T23:57:17Z"

)
copied to clipboard
Get current user #
This function returns the current user object when a user is logged in
auth.currentUser()
Example usage:
final user = auth.currentUser();
copied to clipboard
Example response object:
User(
api: {
"apiURL": "https://example.netlify.com/.netlify/identity",
"_sameOrigin": true,
"defaultHeaders": {}
},
url: "https://example.netlify.com/.netlify/identity",
toke: Token(
access_token: "example-jwt-token",
token_type: "bearer",
expires_in: 3600,
refresh_token: "example-refresh_token",
expires_at: 1526062471000
),
id: "example-id",
aud: "",
role: "",
email: "[email protected]",
confirmed_at: "2018-05-04T23:57:17Z",
app_metadata: {
"provider": "email"
},
user_metadata: {},
created_at: "2018-05-04T23:57:17Z",
updated_at: "2018-05-04T23:57:17Z"
)
copied to clipboard
Update a user #
This function updates a user object with specified attributes
user.update( attributes )
Example usage:
const user = auth.currentUser();

user
.update({ email: "[email protected]", password: "password" })
.then(user => debugPrint("Updated user"))
.catchError((error) {
debugPrint("Failed to update user");
rethrow;
);
copied to clipboard
Example response object:
User(
api: {
"apiURL": "https://example.netlify.com/.netlify/identity",
"_sameOrigin": true,
"defaultHeaders": {}
},
url: "https://example.netlify.com/.netlify/identity",
toke: Token(
access_token: "example-jwt-token",
token_type: "bearer",
expires_in: 3600,
refresh_token: "example-refresh_token",
expires_at: 1526062471000
),
id: "example-id",
aud: "",
role: "",
email: "[email protected]",
confirmed_at: "2018-05-04T23:57:17Z",
app_metadata: {
"provider": "email"
},
user_metadata: {},
created_at: "2018-05-04T23:57:17Z",
updated_at: "2018-05-04T23:57:17Z"

)
copied to clipboard
Get a JWT token #
This function retrieves a JWT token from a currently logged in user
user.jwt()
Example usage:
const user = auth.currentUser();
const jwt = user.jwt();
jwt
.then(response => debugPrint("This is a JWT token"))
.catchError((error) {
debugPrint("Error fetching JWT token");
rethrow;
});
copied to clipboard
Example response object:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MjUyMTk4MTYsInN1YiI6ImE5NG.98YDkB6B9JbBlDlqqef2nme2tkAnsi30QVys9aevdCw debugger eval code:1:43
copied to clipboard
Logout a user #
This function removes the current session of the user and log out the user
user.logout()
Example usage:
const user = auth.currentUser();
user
.logout()
.then(response => debugPrint("User logged out"))
.catchError((error){
debugPrint("Failed to logout user");
rethrow;
});
copied to clipboard
See also #

gotrue
gotrue-js
netlify-identity-widget
micro-api-client-library
Netlify identity docs

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.