-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #907 from hngprojects/feat/fix-authentication
Feat/fix authentication
- Loading branch information
Showing
25 changed files
with
192 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"use server"; | ||
|
||
import axios from "axios"; | ||
|
||
import { Profile, User } from "~/types"; | ||
|
||
interface AuthResponse { | ||
data: User; | ||
access_token: string; | ||
} | ||
|
||
const apiUrl = process.env.API_URL; | ||
|
||
const googleAuth = async (profile: Profile): Promise<AuthResponse> => { | ||
try { | ||
const response = await axios.post(`${apiUrl}/api/v1/auth/google`, { | ||
id_token: profile.id_token, | ||
}); | ||
|
||
return { | ||
data: response.data.user, | ||
access_token: response.data.access_token, | ||
}; | ||
} catch { | ||
throw new Error("Authentication failed"); | ||
} | ||
}; | ||
|
||
export { googleAuth }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"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 { | ||
error: "Login Failed. Please check your email and password.", | ||
}; | ||
} | ||
const { email, password } = validatedFields.data; | ||
const payload = { email, password }; | ||
try { | ||
const response = await axios.post(`${apiUrl}/api/v1/auth/login`, payload); | ||
const access_token = response.data.access_token; | ||
|
||
cookie.set("access_token", access_token, { | ||
maxAge: 60 * 60 * 24 * 1, | ||
httpOnly: true, | ||
path: "/", | ||
priority: "high", | ||
}); | ||
return { | ||
status: response.status, | ||
}; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Login failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; | ||
|
||
export const nextlogin = async (values: z.infer<typeof LoginSchema>) => { | ||
const validatedFields = LoginSchema.safeParse(values); | ||
if (!validatedFields.success) { | ||
return { | ||
error: "Login Failed. Please check your email and password.", | ||
}; | ||
} | ||
const { email, password } = validatedFields.data; | ||
const payload = { email, password }; | ||
try { | ||
const response = await axios.post(`${apiUrl}/api/v1/auth/login`, payload); | ||
|
||
return response.data; | ||
} catch (error) { | ||
return axios.isAxiosError(error) && error.response | ||
? { | ||
error: error.response.data.message || "Login failed.", | ||
status: error.response.status, | ||
} | ||
: { | ||
error: "An unexpected error occurred.", | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/app/dashboard/(admin)/admin/(settings)/settings/organization/export.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/app/dashboard/(user-dashboard)/settings/organization/members/action/member.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.