Renovation
Search…
.auth
Authentication Module is responsible for all authentication functionality. Supported authentication mechanisms are:
  • Simple (Username/email & password)
  • Pin (quick login) using username & pin
  • SMS (OTP) using mobile number & OTP
On change of the session, i.e: user logs in or logs out, SessionStatus, a BehaviorSubject, is updated.

.login() β˜†

To login using username/email and password. This method returns a SessionStatusInfo object.
On successful verification, the SessionStatus is updated with the current user.

Input

LoginParams
property
type
required
description
email
string
yes
The email or username of the user
password
string
yes
The password of the user

Output

SessionStatusInfo

Example

async-await
1
const authResponse = await renovationInstance.auth.login({
2
3
password: "supercomplexpassword"
4
});
5
​
6
console.log("Successful login", authResponse.data.currentUser);
Copied!
Classic Promise
1
renovationInstance.auth
2
.login({
3
4
password: "supercomplexpassword"
5
})
6
.then(authResponse =>
7
console.log("Successful login", authResponse.data.currentUser)
8
);
Copied!

Possible Errors

Incorrect credentials
HTTP code: 401
type: AuthenticationError
cause: Incorrect Password
suggestion: Enter the correct credentials
Non-existing user
HTTP code: 404
type: NotFoundError
cause: User disabled or missing
suggestion: Enter the correct credentials
By default cookies are used for session authentication. If JWT is required, call auth.enableJWT(true) before logging in.

.pinLogin() β˜…

This method is mostly used to quickly login, for instance POS. As with .login(), it returns SessionStatusInfo.
On successful verification, the SessionStatus is updated with the current user.

Input

PinLoginParams
property
type
required
description
username
string
yes
The username of the user
pin
string
yes
The pin of the user (all digits)

Output

SessionStatusInfo

Example

async-await
1
try {
2
const authResponse = await renovationInstance.auth.pinLogin({
3
user: "user1",
4
pin: "123456"
5
});
6
​
7
console.log("Successful login", authResponse.data.currentUser);
8
} catch (err) {
9
console.error("Logging in failed", err);
10
}
Copied!
Classic Promise
1
renovationInstance.auth
2
.login({
3
user: "user1",
4
pin: "123456"
5
})
6
.then(authResponse =>
7
console.log("Successful login", authResponse.data.currentUser)
8
)
9
.catch(err => console.error("Logging in failed", err));
Copied!

Possible Errors

Wrong PIN
HTTP code: 401
type: AuthenticationError
cause: Wrong PIN is entered
suggestion: Re-enter the PIN correctly

.sendOTP() β˜…

To get OTP using mobile number through SMS, this function can be used.
The SMS settings should be configured correctly in the backend before using this function.
This method does not authenticate the user. The next function .verifyOTP() will provide the option to authenticate the user and create a session.

Input

SendOTPParams
property
type
required
description
mobile
string
yes
The mobile number of the user
newOTP
boolean
no
Whether to generate a new OTP or reuse the existing one

Output

SendOTPResponse

Example

async-await
1
const authResponse = await renovationInstance.auth.sendOTP({
2
mobile: "+971000000000"
3
});
4
console.log("OTP sent", authResponse.data.status);
Copied!
Classic Promise
1
renovationInstance.auth
2
.sendOTP({
3
mobile: "+971000000000"
4
})
5
.then(authResponse => console.log("OTP sent", authResponse.data.status));
Copied!

Possible Errors

The errors depends mostly on the SMS provider setup in the backend and would be propagated through this function (API)

.verifyOTP() β˜…

To be used for verifying the OTP received after invoking .sendOTP().
On successful verification, the SessionStatus is updated with the current user.
Unlike .login(), this method will not return a SessionStatusInfo, instead a VerifyOTPResponse is returned.

Input

VerifyOTPParams
property
type
required
description
mobile
string
yes
The mobile number of the user
OTP
string
yes
The OTP received by through SMS
loginToUser
boolean
yes
Whether to generate a new OTP or reuse the existing one
Note: If loginToUser is set to false, the user is not logged in and SessionStatus is not updated. This can be useful in some applications like validating a change of mobile number.

Output

VerifyOTPResponse

Example

async-await
1
const authResponse = await renovationInstance.auth.verifyOTP({
2
mobile: "+971000000000",
3
OTP: "123456",
4
loginToUser: false
5
});
6
if (authResponse.success) {
7
console.log("OTP Verified", authResponse.data.status);
8
} else {
9
console.error("OTP Not Verified", authResponse.error);
10
}
Copied!
Classic Promise
1
renovationInstance.auth
2
.verifyOTP({
3
mobile: "+971000000000",
4
OTP: "123456",
5
loginToUser: false
6
})
7
.then(authResponse => {
8
if (authResponse.success) {
9
console.log("OTP Verified", authResponse.data.status);
10
} else {
11
console.error("OTP Not Verified", authResponse.error);
12
}
13
});
Copied!

Sample Response

1
{
2
"data": {
3
"status": "verified",
4
"mobile": "+971502760387"
5
},
6
"success": true,
7
"httpCode": 200
8
}
Copied!

Possible Errors

Invalid Pin
HTTP code: 401
type: AuthenticationError
cause: Wrong OTP entered
suggestion: Enter correct OTP received by SMS or generate a new OTP
User not found / No Linked User
HTTP code: 404
type: AuthenticationError
cause: User is either not registered or does not have a mobile number
suggestion: Create user or add a mobile number

.checkLogin()

Checks the status of the login first locally and then verifies with the backend. SessionStatusInfo is returned.

Output

SessionStatusInfo

Example

async-await
1
const authResponse = await renovationInstance.auth.checkLogin();
2
if (authResponse.success) {
3
console.log("Logged In", authResponse.data);
4
} else {
5
console.error("Logged Out", authResponse.error);
6
}
Copied!
Classic Promise
1
renovationInstance.auth.checkLogin().then(authResponse => {
2
if (authResponse.success) {
3
console.log("Logged In", authResponse.data);
4
} else {
5
console.error("Logged Out", authResponse.error);
6
}
7
});
Copied!

Sample Response

1
{
2
"home_page": "/desk",
3
"message": "Logged In",
4
"user": "[email protected]",
5
"full_name": "A Sample User",
6
"has_quick_login_pin": false
7
}
Copied!

.getCurrentUserRoles() β˜…

Gets the roles of the current user logged in.
If the user is not logged in, the roles of Guest are loaded by default.

Output

string[]

Example

async-await
1
const userRoles = await renovationInstance.auth.getCurrentUserRoles();
2
if (userRoles.success) {
3
console.log("User Roles loaded", userRoles.data);
4
} else {
5
console.error("User roles failed", userRoles.error);
6
}
Copied!
Classic Promise
1
renovationInstance.auth.getCurrentUserRoles().then(userRoles => {
2
if (userRoles.success) {
3
console.log("User Roles loaded", userRoles.data);
4
} else {
5
console.error("User roles failed", userRoles.error);
6
}
7
});
Copied!

Sample Response

1
{
2
"success": true,
3
"httpCode": 200,
4
"data": [
5
"Translator",
6
"Renovation User",
7
"Blogger",
8
"All",
9
"Guest"
10
]
11
}
Copied!

.logout()

Logs out the user locally followed by ending the session in the backend.

Output

any

Example

async-await
1
const response = await renovationInstance.auth.logout();
2
if (response.success) {
3
console.log("Logged out", response.data);
4
} else {
5
console.error("Error", response.error);
6
}
Copied!
Classic Promise
1
renovationInstance.auth.getCurrentUserRoles().then(response => {
2
if (response.success) {
3
console.log("Logged out", response.data);
4
} else {
5
console.error("Error", response.error);
6
}
7
});
Copied!

Sample Response

1
{
2
"success": true,
3
"httpCode": 200,
4
"data": {}
5
}
Copied!
Even if the server's response is a failure, the user will need to re-login since the user is logged out in the front-end.

Reference

SessionStatusInfo

property
type
description
loggedIn
boolean
Whether the user is logged in
timestamp
number
The timestamp when the SessionStatusInfo was updated
currentUser
string
The current user logged in, if any
In addition, the interface contains an indexed property ([x: string]: any), containing information such as full_name.
Last modified 1yr ago