added next-intl, composed next-auth and next-intl in middleware, fixed some bugs, completed details page, added google auth

This commit is contained in:
Alibek committed 2024-02-29 14:09:37 +06:00
1 parent 9cd7b2a67d
commit 6637ab4eae
172 files changed
+1772 -502

No files matched your search

+68 -23
View File
@@ -2,24 +2,45 @@ import axios from "axios";
import { AuthOptions } from "next-auth";
import { JWT } from "next-auth/jwt";
import CredentialsProvider from "next-auth/providers/credentials";
import { apiInstance } from "./apiConfig";
import { IRefresh, ITokens } from "../types/token-type";
import GoogleProvider from "next-auth/providers/google";
interface IToken {
access: string;
}
const verifyToken = async (access_token: string) => {
const res = await apiInstance.post("/token/verify/", {
token: access_token,
});
if ([200, 201].includes(res.status)) {
return true;
}
return false;
};
const refreshToken = async (token: JWT): Promise<JWT> => {
const data = {
refresh: token.refresh_token,
};
// const date = new Date().toLocaleTimeString();
// const expire = new Date(token.expires_in).toLocaleTimeString();
const response = await axios.post<IToken>(
"https://api.kgroaduat.fishrungames.com/api/v1/token/refresh/",
const verify = await verifyToken(token.access_token);
if (verify)
return {
...token,
};
const response = await apiInstance.post<IRefresh>(
"/users/refresh/",
data
);
return {
...token,
access_token: response.data.access,
expires_in: response.data.expires_at,
};
};
@@ -27,7 +48,6 @@ export const authConfig: AuthOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: {
label: "Email",
@@ -36,35 +56,30 @@ export const authConfig: AuthOptions = {
},
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
if (!credentials?.email || !credentials?.password)
return null;
const { email, password } = credentials as any;
const data = {
email,
password,
};
const res = await fetch(
"https://api.kgroaduat.fishrungames.com/api/v1/users/login/",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email,
password,
}),
}
);
const res = await apiInstance.post("/users/login/", data);
if (res.status.toString()[0] === "4") {
if (![200, 201].includes(res.status)) {
return null;
}
const user = await res.json();
const user = res.data;
return user;
},
}),
GoogleProvider({
clientId: process.env.CLIENT_ID as string,
clientSecret: process.env.CLIENT_SECRET as string,
}),
],
pages: {
signIn: "/sign-in",
@@ -73,9 +88,39 @@ export const authConfig: AuthOptions = {
strategy: "jwt",
},
callbacks: {
async jwt({ token, user }) {
async signIn({ account, profile, user }) {
if (!profile?.email) {
throw new Error("No Profile");
}
if (account?.provider === "google") {
const data = {
auth_token: account?.id_token,
};
const res = await apiInstance.post<ITokens>(
"/users/google/",
data
);
if (![200, 201].includes(res.status)) {
return false;
}
user.access_token = res.data.access_token;
user.refresh_token = res.data.refresh_token;
user.expires_in = res.data.expires_in;
}
return true;
},
async jwt({ token, user, account }) {
if (user) return { ...token, ...user };
if (token.exp) {
return token;
}
return refreshToken(token);
},