Skip to content

Commit

Permalink
Merge branch 'dev' into feat/notification-system-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
ikennarichard authored Aug 1, 2024
2 parents 68c4e4c + 3fd2f5b commit 0275ba5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/config/auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NextAuthConfig, Session } from "next-auth";
import { JWT } from "next-auth/jwt";
import Credentials from "next-auth/providers/credentials";
import Google from "next-auth/providers/google";
import { cookies } from "next/headers";

import { LoginSchema } from "~/schemas";
import { CustomSession } from "~/types";
Expand All @@ -22,6 +23,7 @@ interface Profile {
}

interface User {
access_token: string;
id: string;
email: string;
fullname: string;
Expand All @@ -40,6 +42,7 @@ interface ApiResponse {
message: string;
user: User;
data: Data;
access_token: string;
}

export default {
Expand Down Expand Up @@ -103,6 +106,7 @@ export default {
session: Session;
token: JWT;
}): Promise<CustomSession> {
const authToken = cookies()?.get("access_token")?.value;
session.user = {
id: token.id as string,
first_name:
Expand All @@ -116,20 +120,13 @@ export default {
email: token.email as string,
image: token.picture || (token.avatar_url as string),
role: token.role as string,
access_token: token.access_token as string,
access_token: (token.access_token as string) || (authToken as string),
};

session.access_token =
(token.access_token as string) || (authToken as string);
return session as CustomSession;
},
// async redirect({ url, baseUrl }) {
// if (url === "/login") {
// return baseUrl;
// }
// if (url === `${baseUrl}/api/auth/signout`) {
// return baseUrl;
// }
// return "/dashboard";
// },
},
pages: {
signIn: "/login",
Expand Down
1 change: 1 addition & 0 deletions src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ declare module "next-auth" {
role: User["role"];
access_token: User["access_token"];
} & DefaultSession["user"];
access_token?: string;
}
}
17 changes: 16 additions & 1 deletion src/utils/googleAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";

import axios from "axios";
import { cookies } from "next/headers";

const apiUrl = process.env.API_URL;
interface Profile {
Expand All @@ -22,7 +23,21 @@ const googleAuth = async (profile: Profile) => {
id_token: profile.id_token,
});

return response.data;
const access_token =
response.data.access_token ?? response.data.data.access_token;
const cookie = cookies();
cookie.set("access_token", access_token, {
maxAge: 60 * 60 * 24 * 1, // 1 day
httpOnly: true,
path: "/",
priority: "high",
});
return {
data: response.data,
user: response.data.user,
access_token:
response.data.access_token ?? response.data.data.access_token,
};
} catch (error) {
return error;
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/login.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
"use server";

import axios from "axios";
import { cookies } from "next/headers";
import * as z from "zod";

import { LoginSchema } from "~/schemas";

const apiUrl = process.env.API_URL;

export const loginUser = async (values: z.infer<typeof LoginSchema>) => {
const cookie = cookies();
const validatedFields = LoginSchema.safeParse(values);
if (!validatedFields.success) {
return {
Expand All @@ -18,6 +20,15 @@ export const loginUser = async (values: z.infer<typeof LoginSchema>) => {
const payload = { email, password };
try {
const response = await axios.post(`${apiUrl}/api/v1/auth/login`, payload);
const access_token =
response.data.access_token ?? response.data.data.access_token;

cookie.set("access_token", access_token, {
maxAge: 60 * 60 * 24 * 1,
httpOnly: true,
path: "/",
priority: "high",
});
return {
status: response.status,
data: response.data,
Expand Down

0 comments on commit 0275ba5

Please sign in to comment.