-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
executable file
·159 lines (138 loc) · 4.04 KB
/
auth.ts
File metadata and controls
executable file
·159 lines (138 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import NextAuth, { DefaultSession } from "next-auth";
import SpotifyProvider from "next-auth/providers/spotify";
import CredentialsProvider from "next-auth/providers/credentials";
import { cookies } from 'next/headers';
import {
AuthResponse,
MemberData,
UserType,
AuthState
} from '@/app/lib/types';
import { AUTH_CONSTANTS } from '@/app/constants/auth';
// Extend the built-in session type
declare module "next-auth" {
interface Session extends DefaultSession {
accessToken?: string;
user: MemberData & DefaultSession["user"];
}
interface User extends MemberData {}
}
type LoginCredentials = {
email: string;
password: string;
}
// Initial auth state
export const initialAuthState: AuthState = {
user: null,
isAuthenticated: false,
isLoading: false,
error: null
};
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
SpotifyProvider({
clientId: process.env.SPOTIFY_CLIENT_ID!,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET!,
}),
CredentialsProvider({
name: 'Credentials',
credentials: {
email: {
label: "Email",
type: "email",
placeholder: "Enter your email"
},
password: {
label: "Password",
type: "password",
placeholder: "Enter your password"
}
},
async authorize(credentials): Promise< any|LoginCredentials| {} > {
if (!credentials?.email || !credentials?.password) {
throw new Error('Please provide both email and password');
}
try {
const user = await loginUser({
email: credentials.email,
password: credentials.password
});
return user;
} catch (error) {
throw new Error(error instanceof Error ? error.message : 'Invalid credentials');
}
},
}),
],
callbacks: {
async jwt({ token, user }) {
if (user) {
token.accessToken = user.Token;
token.userData = user;
}
return token;
},
async session({ session, token }: { session: any; token: any }) {
session.accessToken = token.accessToken;
session.user = token.userData as MemberData;
return session;
},
async redirect({ url, baseUrl }) {
// Handle redirect URLs
if (url.startsWith(baseUrl)) return url;
else if (url.startsWith("/")) return new URL(url, baseUrl).toString();
return baseUrl;
}
},
pages: {
signIn: '/auth/signin',
error: '/auth/error',
newUser: '/auth/register'
},
session: {
strategy: "jwt",
maxAge: 24 * 60 * 60, // 24 hours
},
debug: process.env.NODE_ENV === 'development',
});
async function loginUser(credentials: any): Promise<MemberData|null> {
const formData = new FormData();
formData.append('id', credentials.email);
formData.append('password', credentials.password);
formData.append('API_KEY', AUTH_CONSTANTS.API_KEY);
const response = await fetch('https://singnify.com/api/v2/php/login.php', {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data: AuthResponse = await response.json();
if (data.status !== '200' || !data.member_data?.Token) {
throw new Error(data.message || 'Authentication failed');
}
// Store in server-side cookies
if (data.status === '200') {
// Store auth token in server-side cookies
const cookieStore = await cookies();
cookieStore.set('auth_token', data.member_data.Token, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 86400, // 1 day
});
return data.member_data;
}
return null;
}
// Server-side logout function
async function logoutUser(): Promise<void> {
cookies();
await signOut({ redirectTo:'/auth/signin' });
}
// Helper function to get current session with proper typing
async function getCurrentUser(): Promise<MemberData | null> {
const session = await auth();
return session?.user ?? null;
}
export { logoutUser, getCurrentUser }