Authentication

The Anthentication is a class to manage login/logout and session. To use it you must create a class inside src/security. Call it whenever you want, but in this case we'll call it CustomAuthenticator

import Auth from "../Plasma/Security/Auth";
import Session from "../Plasma/Security/Session";
import Ajax from "../Plasma/Utils/Ajax";

export default class CustomAuthenticator extends Auth {
    
    async login(email, password) {
        return await Ajax.post(PLASMA.getConfig().host + "myroutetologin", {
            userName: email,
            password: password
        }).then((res) => {
            //if the result from your backend is positive
            Session.setSession(res.user.authToken, res.user.userName)
            return true;
            //if your result from your backend is negative
            return false;
        })
    }

    async logout() {
        return await Ajax.post(PLASMA.getConfig().host + "myroutetologout", {
            authToken: Session.getToken()
        }).then((res) => {
            Session.clearSession()
            return true;
        })
    }

}

Here we have a non-working exemple of CustomAuthenticator

First thing you see is that class extend Auth who contain 2 abstract methods: login(<email or username> , password) and logout() both of them return promise We write the code we want inside the login and logout methods using Ajax handler and we use Session to do what we want

Warning

This code is "pseudo-code" containing error specially on login method. As Plasma is writed to work with all type of backend you'll need to write the whole login method matching your backend output

Last updated