Addional Methods

These are the underhood methods that redshield auth uses to provide the authentication as a service. Using these methods you can build your own authentication mechanism without relying on the default authentication UI provided by redshield itself.

1. login

This is the core login method used in redshield auth. All You have to do is pass the object consisting email and password to the login method. This method returns a promise so always use it in async function blocks.

login.ts

import { login } from "redshield";

const handleLogin = async (formData: FormData) => {
  const email = formData.get("email") as string;
  const password = formData.get("password") as string;
  const res = await login({ email: email, password: password }); //returns {status: boolean , message: string }
};

2. register

This methods is used to register a new user to your application. You dont have to do any checks if user is already registered or not All you need to do is call the register method by providing object of email and password. This also return a promise.

register.ts

import { register } from "redshield";

const handleRegister = async (formData: FormData) => {
  const email = formData.get("email") as string;
  const password = formData.get("password") as string;
  const res = await register({ email: email, password: password }); //returns {status: boolean , message: string }
};

Note : make sure to verify user email by following methods before registering user.


3. sendEmailVerificationCode

This method allows you to send email to your user consisting of a server generated 6 digit code for verification. This code can be later verified with verifyVerificationCode method. The code sent is only valid for 3 minutes.

sendVerification.ts

import { sendEmailVerificationCode } from "redshield";

const handleSendCode = async (formData: FormData) => {
  const email = formData.get("code_email") as string;
  const res = await sendEmailVerificationCode({ email: email }); //returns {status: boolean , message: string }
};

Note : Don't send codes continuously as this method has limits.


4. verifyVerificationCode

This method verifies the code sent by thesendEmailVerificationCode method.

verifyVerificationCode.ts

import { verifyVerificationCode } from "redshield";

const handleVerifyCode = async (formData: FormData) => {
  const code = formData.get("verification_code") as string;
  const email = formData.get("email") as string;
  const res = await verifyVerificationCode({
    email: email,
    code: code,
  });    //returns {status: boolean , message: string }
};

5. getSession

This is the core method for getting the session of current user. It has all the information related to authentication state of user. The main response by this method consits of a object that has status property which indicates the status of the user. If it returns true then the user is authenticated else the user is not authenticated.
This also returns some additional information with the status property as data. data has information like email , isAdmin etc,

ProtectedPage.tsx

import { getSession } from "redshield";

const ProtectedPage = async () => {
  const session = await getSession(); // returns { status: boolean , message: string , data: {email: string , isAdmin: boolean}}
  if(session.status){
    return(
        <div>
           <p>user is logged in as {session.data.email}</p>
           <p>roll is {session.data.isAdmin ? "admin" : "user"}</p>
        </div>
    );
  }
  return(
    <div>
        <p>user is not logged in. Please log in to access this page</p>
    </div>
  )
}
export default ProtectedPage;

You can manage the user roles in your redshield app dashboard by a single click