Skip to content

Commit

Permalink
Merge pull request #73 from Princekumarofficial/main
Browse files Browse the repository at this point in the history
Google Authentication
  • Loading branch information
AryanGKulkarni authored Jun 19, 2024
2 parents 8d4bc50 + 7cbd848 commit 599801a
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 66 deletions.
44 changes: 44 additions & 0 deletions public/google_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/icons/lock.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions src/components/loginForms/googleLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from "react";
import GoogleButton from "react-google-button";
import { googleLoginURL } from "@/helpers/googleAuth";
import googleLogo from "@/../public/google_icon.svg";

const GoogleLoginButton = ({ onClick }) => {
return (
<button
className="bg-white h-2rem pl-2 pr-4 flex items-center border-2 border-gray-200 rounded-xl hover:shadow-inner transition-shadow"
onClick={onClick}
>
<img
src={googleLogo.src}
alt=""
className="aspect-square h-12 float-left"
/>
Login With google
</button>
);
};

const GoogleLogin = () => {
const loginURL = googleLoginURL();
return (
<div>
<GoogleLoginButton
onClick={() => {
window.location.href = loginURL;
}}
/>
</div>
);
};

export default GoogleLogin;
23 changes: 0 additions & 23 deletions src/components/loginForms/googleLoginButton.tsx

This file was deleted.

79 changes: 43 additions & 36 deletions src/components/loginForms/loginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import toast, { Toaster } from "react-hot-toast";
import { useRouter } from "next/navigation";
import Cookies from "js-cookie";
import { jwtDecode } from "jwt-decode";
import GoogleLogin from "./googleLogin";
import { LoginWithEmail } from "@/components/loginForms/loginWithEmail";

const LoginForm = () => {
const [email, setemail] = useState<String | null>(null);
const [role, setrole] = useState<String | null>("STUDENT");
Expand All @@ -18,42 +21,44 @@ const LoginForm = () => {
<div className="bg-white p-4 px-4 md:p-8 rounded-md">
<div className="w-full">
<div className="lg:col-span-2 w-full">
<div className="my-2 w-full">
<div className="md:col-span-5">
<input
type="text"
name="email"
id="email"
className="h-10 border mt-1 rounded px-4 w-full bg-gray-50"
placeholder="[email protected]"
required={true}
onChange={(e) => {
setemail(e?.target?.value);
}}
/>
</div>
<div className="md:col-span-5">
<select
name="role"
id="role"
className="h-10 border mt-1 rounded px-4 w-full bg-gray-50"
required={true}
onChange={(e) => {
setrole(e?.target?.value);
}}
>
<option>Student</option>
<option>Faculty</option>
<option>Recruiter</option>
<option>Manager</option>
<option>Admin</option>
</select>
<div className="my-2 w-full h-max">
<div className="mb-4">
<div className="md:col-span-5">
<input
type="text"
name="email"
id="email"
className="h-10 border mt-1 rounded px-4 w-full bg-gray-50"
placeholder="[email protected]"
required={true}
onChange={(e) => {
setemail(e?.target?.value);
}}
/>
</div>
<div className="md:col-span-5">
<select
name="role"
id="role"
className="h-10 border mt-1 rounded px-4 w-full bg-gray-50"
required={true}
onChange={(e) => {
setrole(e?.target?.value);
}}
>
<option>Student</option>
<option>Faculty</option>
<option>Recruiter</option>
<option>Manager</option>
<option>Admin</option>
</select>
</div>
</div>

<div className="md:col-span-5 text-right">
<div className=" items-center flex justify-center">
<div className="items-center flex justify-center flex-col gap-4">
<button
className="bg-blue-500 my-5 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
onClick={() => {
if (email == null || email.length == 0) {
toast.error("Email is Required");
Expand All @@ -71,18 +76,18 @@ const LoginForm = () => {
response.data.accessToken,
{
expires: 365,
},
}
);
Cookies.set(
"user",
JSON.stringify(
jwtDecode(response.data.accessToken),
jwtDecode(response.data.accessToken)
),
{ expires: 365 },
{ expires: 365 }
);
toast.success("logged in");
window.location.href = "/";
},
}
)
.catch((err) => {
alert(err);
Expand All @@ -92,6 +97,8 @@ const LoginForm = () => {
>
Request Access
</button>
<LoginWithEmail email={email} />
<GoogleLogin />
</div>
</div>
</div>
Expand Down
37 changes: 37 additions & 0 deletions src/components/loginForms/loginWithEmail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";
import { url } from "../../helpers/api";
import LockImg from "@/../public/icons/lock.svg";
import toast from "react-hot-toast";

export const LoginWithEmail = (params: { email: String }) => {
const onClick = async () => {
const res = await fetch(url("/auth/passwordless"), {
method: "POST",
cache: "no-store",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ email: params.email }),
});
console.log(res);
const resOk = res.ok;
if (resOk) toast.success("Email Has Been Sent");
return resOk;
};

return (
<>
<div>
<button
className="bg-white h-2rem px-4 py-2 flex items-center border-2 border-gray-200 rounded-xl hover:shadow-inner transition-shadow"
onClick={onClick}
>
<span>
<img src={LockImg.src} className="h-8 mr-1" alt="" />
</span>
Login With Email
</button>
</div>
</>
);
};
11 changes: 11 additions & 0 deletions src/helpers/googleAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const baseUrl = process.env.NEXT_PUBLIC_BACKEND_URL;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

export const url = (NextUrl: string) => {
return `${baseUrl}/api/v1${NextUrl}`;
};

export const googleLoginURL = () => {
const googleLoginURL = url("/auth/google/login");
return googleLoginURL;
};
31 changes: 24 additions & 7 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@ import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

const adminRoutes = ["/admin/company", "/admin/students", "/admin/job"];
const studentRoutes = ['/student/jobs', '/student/offCampus', '/student/onCampus', '/student/interviewExperiences', '/student/profile', '/student/resumes'];
const recruiterRoutes = ['/recruiter/jaf', '/recruiter/prevjaf'];
const studentRoutes = [
"/student/jobs",
"/student/offCampus",
"/student/onCampus",
"/student/interviewExperiences",
"/student/profile",
"/student/resumes",
];
const recruiterRoutes = ["/recruiter/jaf", "/recruiter/prevjaf"];

export function middleware(request: NextRequest) {
const userCookie = request.cookies.get("user");
const user = userCookie ? JSON.parse(userCookie.value) : null;;

if (user?.role !== "ADMIN" && adminRoutes.includes(request.nextUrl.pathname) ) {
const user = userCookie ? JSON.parse(userCookie.value) : null;

if (
user?.role !== "ADMIN" &&
adminRoutes.includes(request.nextUrl.pathname)
) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (user?.role !== "STUDENT" && studentRoutes.includes(request.nextUrl.pathname) ) {
if (
user?.role !== "STUDENT" &&
studentRoutes.includes(request.nextUrl.pathname)
) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (user?.role !== "RECRUITER" && recruiterRoutes.includes(request.nextUrl.pathname) && request.url.includes("/recruiter") ) {
if (
user?.role !== "RECRUITER" &&
recruiterRoutes.includes(request.nextUrl.pathname) &&
request.url.includes("/recruiter")
) {
return NextResponse.redirect(new URL("/login", request.url));
}

Expand Down

0 comments on commit 599801a

Please sign in to comment.