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 }
};
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.
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.
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 }
};
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