Authentication
RemBase is a cross platform backend development system that helps you create fast and secure backend that reduces your backend development time by 70%.
Prerequsite
Install and initialize the application. see getting started
Anonymous Login
Anonymous is one of the most easiest authentication method available in the market but should be used with causion as it is not a reliable type of authentication for most cases.
// let app = await initializeRembaseSync(<appId>,<selfHostUrl>?)
async function yourOwnLoginFunction(){
let credential = CredentialTypes.anonymous()
let user = await app.login(credential,<authKey>)
}
authKey : (optional) A unique 32 bit key. If you are using self hosted backend you need to pass this field.
authKey is used to verify your users at your custom backend and can be found in .env file.
Email password Login
To use email password login you must register and verify the user first.
Register
let await = app.registerUser(email,password)
upon successful registration an email will be sent to the user. You might want to prompt user to check their email and spam
To send verification email again use
app.resendEmail(email,method) // method can be 'token' or 'otp'
A verification email will be sent to User that will look like the following.
https://yourWebsite.com/yourVerificationPage?token=token
Once the user is successfully registered you can continue with the Login
const credential = CredentialTypes.email_password(email,password)
const result = await app.login(credential)
Email OTP Login
You can also sign-in user with thier email and OTP. Here's how you can do it.
Step1. Send the otp to the user's email
app.send_email_otp(email)
Step2. You can use that OTP and the email to login to the User.
const credential = CredentialTypes.email_otp(email)
const result = await app.login(credential)
Note: If you are using your own email credentials you might want to prompt users to check their email also.
Google Login
Prerequsite
Go to google cloud console and create a OAuth ClientId and include your development host eg. localhost:3000 for development purposes
let credential = CredentialTypes.google(<clientId>)
let user = await app.login(credential)
To use google login you must go to Google cloud console and create a client Id see video (opens in a new tab)
Phone Auth
phone auth requires two steps
step1. sending OTP
Please make sure the phone contains the country code eg. +1
app.send_phone_otp(<phoneNumber>)
once the user recieves the OTP take the otp and call
const credential = CredentialTypes.phone(<phoneNumber>,<OTP>);
const user = await app.login(credential)
LogOut
use the code snippet below to log out user.
app.currentUser.logOut()
//your code