Login / Logout
Once a user has signed up, they can log in to your service using their email and password.
Login
Use the login()
method to log a user into your service.
If the login is not successful due to invalid password, or user may not have confirm their signup etc... the login()
method will throw an error.
When successful, it will respond with the UserProfile
object.
WARNING
If signup_confirmation
option was set to true
during signup()
, users will not be able to log in until they have confirmed their account.
INFO
When the user has successfully confirmed their signup and logged in, they will be sent a welcome email. You can also customize the email template for the signup confirmation email.
For more info on email templates, see Automated E-Mail.
Below is an example of a login form that uses the login()
method. When the user successfully logs in, they will be redirected to the welcome.html
page.
<form action='welcome.html' onsubmit="skapi.login(event).catch(err=>alert(err.message))">
<h2>Login</h2>
<hr>
<label>
Email<br>
<input type="email" name="email" placeholder="user@email.com" required>
</label><br><br>
<label>
Password<br>
<input id="password" type="password" name="password" placeholder="Your password" required>
</label><br><br>
<input type="submit" value="Login">
</form>
let parameters = {
email: 'user@email.com',
password: 'password'
}
skapi.login(parameters)
.then(user => window.href = 'welcome.html');
For more detailed information on all the parameters and options available with the login()
method, please refer to the API Reference below:
login(params): Promise<UserProfile>
Getting User Information
When user is logged in to your service, you can retrieve their information from user
property of the Skapi object. This property is getter-only, and will not be object reference.
It returns the UserProfile object.
console.log(skapi.user); // null when user is logged out, User's information object when logged in.
Requesting User Information
The getProfile()
method allows you to retrieve the user's information from the backend. It returns the UserProfile object.
If the user is not logged in, getProfile()
returns null
.
skapi.getProfile().then(profile=>{
console.log(profile); // User's information
if(profile === null) {
// The user is not logged in
}
})
For more detailed information on all the parameters and options available with the getProfile()
method, please refer to the API Reference below:
getProfile(options?): Promise<UserProfile | null>
Auto Login
By default, once user login to your website, their login session is maintained until they logout.
To ensure that users' sessions are destroyed when they leave your website, you can set options.autoLogin to false in the third argument when initializing Skapi.
const options = {
autoLogin: false, // set to true to maintain the user's session
};
//Set the third argument as options
const skapi = new Skapi('service_id', 'owner_id', options);
Listening to Login Status
You can listen to the login status of the user by setting a callback function in the option.eventListener.onLogin
option argument of the constructor argument in Skapi.
The onLogin
callback function will be called whenever the user logs in, logs out, or even when their profile is updated.
The callback function will receive the UserProfile object as an argument.
const options = {
eventListener: {
onLogin: (profile) => {
console.log(profile); // null when user is logged out, User's information object when logged in.
}
}
};
const skapi = new Skapi('service_id', 'owner_id', options);
You can also add multiple event listeners to the onLogin
event after the Skapi object has been initialized.
skapi.onLogin = (profile) => {
console.log(profile); // null when user is logged out, User's information object when logged in.
}
This handler can be useful for updating the UI when the user logs in or logs out.
Logout
The logout()
method logs the user out from the service.
<form onsubmit='skapi.logout(event)' action='page_to_show_after_logout.html'>
<input type='submit' value='Logout'>
</form>
skapi.logout().then(res=>{
console.log(res); // 'SUCCESS: The user has been logged out.'
window.location.replace("page_to_show_after_logout.html");
});
For more detailed information on all the parameters and options available with the logout()
method, please refer to the API Reference below:
logout(event?:SubmitEvent): Promise<string>
TIP
For convenience, event
:SubmitEvent argument is there just to use with <form>
element. You can use action
attributes in form to redirect user after logout.