Generic Authentication Utility Module
A collection of JavaScript functions to handle authentication tasks such as sign up, sign in, and refresh access token.
Motivation
To reuse the same collection of authentication utility functions across all projects powered by FanScore.
Getting Started
npm i @incrowd/auth-util
or yarn add @incrowd/auth-util
then in JavaScript:
import authUtil from '@incrowd/auth-util'
const config = {
// Optional
fetchStart: () => {
// Logic to be called when fetch start
},
// Optional
fetchEnd: () => {
// Logic to be called when fetch end
},
authObj: () => {
// return authObj
},
deviceId: () => {
// return deviceId
},
clientId: () => {
// return clientId
},
storeAuthObj: newAuthObj => {
// Logic to store newAuthObj
},
baseUrl: {
auth: () => {
// return BASE_AUTH_API_URL
},
profile: () => {
// return BASE_PROFILE_API_URL
}
}
}
const icAuth = authUtil(config)
API
simpleFetch (url, fetchStart = this.config.fetchStart, fetchEnd = this.config.fetchEnd)
icAuth.simpleFetch(url).then(r => {
// Your logic to handle respond
})
Fetch from url
using GET
method without access token and return a Promise
.
genericFetch (url, method, headers, body, fetchStart = this.config.fetchStart, fetchEnd = this.config.fetchEnd, tryAgain = false)
icAuth.genericFetch(url, 'POST', {
'Content-Type': 'application/json'
}, body).then(r => {...})
Generic fetch method, mostly used by other methods within this library.
fetchWithAuth (url, method = 'GET', body, fetchStart, fetchEnd, token = this.config.authObj().access_token, tryAgain = true, language)
icAuth.fetchWithAuth(url).then(r => {...})
icAuth.fetchWithAuth(url, 'POST', {examplePayload: 'example'}).then(r => {...})
Fetch with access token and return a Promise
. If fetch failed due to 401 Unauthorized
, by default it will refreshToken
and tryAgain
once.
refreshToken ()
icAuth.refreshToken().then(r => {...})
Refresh access token, call storeAuthObj
with newAuthObj
and return a Promise
.
getUser ()
icAuth.getUser()
Decode JWT token and return user object. null
is returned if authentication object does not exist in store.
fetchOauth (body, fetchStart, fetchEnd)
icAuth.fetchOauth(bodyObj).then(r => {...})
Fetch with Content-Type
being application/x-www-form-urlencoded
and return a Promise
. Normally will not call this function directly. This is used in login
, register
, and getAnonymousToken
.
login (email, password, type = 'password', fetchStart, fetchEnd, organisation)
icAuth.login(email, password).then(r => {...})
Log in and return a Promise
, the respond typically contains a new authentication object.
register (email, password, organisation, fetchStart, fetchEnd)
icAuth.register(email, password).then(r => {...})
Register and return a Promise
, the respond typically contains a new authentication object.
getAnonymousToken (fetchStart, fetchEnd)
icAuth.getAnonymousToken().then(r => {...})
Return a Promise
and the respond typically contains anonymous token.
convertBodyToFormData (body)
icAuth.convertBodyToFormData(bodyObj)
Normally will not call this function directly. This function will convert object key and value using encodeURIComponent
.
resendVerifyEmail (fetchStart, fetchEnd)
icAuth.resendVerifyEmail().then(r => {...})
Resend verification email to current signed in user and return a Promise
.
forgotPassword (email, fetchStart, fetchEnd)
icAuth.forgotPassword(email).then(r => {...})
Submit a reset password email request to the back end with email
then return a Promise
. User should then receive an reset password email allowing them to reset their password.
resetPassword (id, code, body, fetchStart, fetchEnd)
icAuth.resetPassword(usedId, resetPasswordCode, {newPassword}).then(r => {...})
Given the correct reset password code
, submit body
containing newPassword
to reset password for user with given user id
then return a Promise
.
changePassword (body, fetchStart, fetchEnd)
icAuth.changePassword({oldPassword, newPassword}).then(r => {...})
Change user password with body
object containing oldPassword
and newPassword
then return a Promise
.
fetchProfile (fetchStart, fetchEnd)
icAuth.fetchProfile().then(r => {...})
Return a Promise
, the respond typically contains a profile object.
updateProfile (body, method = 'PUT', fetchStart, fetchEnd)
icAuth.updateProfile({profilePicture, screenName, firstName, lastName}).then(r => {...})
Update user profile with body
typically contains profilePicture
, screenName
, firstName
, and lastName
then return a Promise
. profilePicture
should be a string represents the image in base64 format without data:image/png;base64,
in front. Default method is PUT
which will replace the whole user profile, PATCH
is also supported by the back end to update specific fields of the profile.
fetchContactPreferences (clientIds = ['INCROWD', this.config.clientId() || 'INCROWD'], fetchStart, fetchEnd)
icAuth.fetchContactPreferences().then(r => {...})
Return a Promise
, the respond typically contains an array of contact preferences. Can pass in an array of client IDs, and duplication will be removed.
updateContactPreferences (body, clientId = this.config.clientId() || 'INCROWD', key = 'CLIENT', fetchStart, fetchEnd)
icAuth.updateContactPreferences({email: true, phone: false}).then(r => {...})
Update contact preferences for a given clientId
. key
can be "CLIENT"
, "SUPPORTED_CLUB"
, or "SPONSOR"
. The body
typically contains email
, sms
, post
, and phone
, and they should all be Boolean.