-
Notifications
You must be signed in to change notification settings - Fork 9
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 #21 from wpsadi/appwrite-auth
Appwrite auth
- Loading branch information
Showing
15 changed files
with
176 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
# Appwrite | ||
APPWRITE_PROJECT_ID = "5f5f3d5d5f5f3" | ||
APPWRITE_ENDPOINT = "https://appwrite.io/v1" | ||
APPWRITE_KEY = "G" | ||
APPWRITE_KEY = "G" | ||
|
||
|
||
# Vercel | ||
HTTPS = 1 # 0 for http, 1 for https | ||
VERCEL_URL = localhost:3000 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ClientAW } from "@/appwrite_configs/config"; | ||
|
||
import { errorHandler, successHandler } from "../../handler"; | ||
|
||
export async function POST() { | ||
try { | ||
const { account } = await ClientAW(true); | ||
await account.deleteSession("current"); | ||
return successHandler({}, "Logged out successfully", 200); | ||
} catch (e) { | ||
return errorHandler(e); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { NextRequest } from "next/server"; | ||
import { AppwriteException } from "node-appwrite"; | ||
|
||
import { errorHandler, successHandler } from "@/app/api/handler"; | ||
import { ClientAW } from "@/appwrite_configs/config"; | ||
import { recoveryPasswordConfirmSchema } from "@/validations/user/recoveryPasswordConfirm"; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
const queryStore = req.nextUrl.searchParams; | ||
|
||
const isPresent = queryStore.has("secret") && queryStore.has("userId"); | ||
|
||
if (!isPresent) { | ||
return errorHandler( | ||
new AppwriteException("userId or secret not present", 400) | ||
); | ||
} | ||
|
||
const secret = queryStore.get("secret") as string; | ||
const userId = queryStore.get("userId") as string; | ||
|
||
// getting new password | ||
let body = { | ||
userId: null, | ||
}; | ||
try { | ||
body = await req.json(); | ||
} catch { | ||
throw new AppwriteException("No body passed", 400); | ||
} | ||
|
||
const validation = recoveryPasswordConfirmSchema.safeParse(body); | ||
|
||
if (!validation.success) { | ||
throw new AppwriteException(validation.error.errors[0].message, 400); | ||
} | ||
|
||
const validatedData = validation.data; | ||
|
||
const { account } = await ClientAW(false); | ||
|
||
await account.updateRecovery(userId, secret,validatedData.newPassword); | ||
|
||
return successHandler( | ||
{ | ||
id: userId, | ||
}, | ||
"Passwrord reset successfully", | ||
201 | ||
); | ||
} catch (e) { | ||
return errorHandler(e); | ||
} | ||
} |
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,34 @@ | ||
import { NextRequest } from "next/server"; | ||
import { AppwriteException } from "node-appwrite"; | ||
|
||
import { errorHandler, successHandler } from "@/app/api/handler"; | ||
import { ClientAW } from "@/appwrite_configs/config"; | ||
import { recoveryPasswordSchema } from "@/validations/user/recoveryPassword"; | ||
|
||
export async function POST(req: NextRequest) { | ||
try { | ||
let body = {}; | ||
try { | ||
body = (await req.json()) || {}; | ||
} catch { | ||
throw new AppwriteException("No body passed", 400); | ||
} | ||
|
||
const validation = recoveryPasswordSchema.safeParse(body); | ||
if (!validation.success) { | ||
throw new AppwriteException(validation.error.errors[0].message, 400); | ||
} | ||
|
||
const { account } = await ClientAW(true); | ||
|
||
const validatedData = validation.data; | ||
await account.createRecovery( | ||
validatedData.email, | ||
"http://localhost:3000/api/user/recovery/password/confirm" | ||
); | ||
|
||
return successHandler({}, "Recovery email sent successfully", 201); | ||
} catch (e) { | ||
return errorHandler(e); | ||
} | ||
} |
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,13 @@ | ||
import { ClientAW } from "@/appwrite_configs/config"; | ||
|
||
import { errorHandler, successHandler } from "../../handler"; | ||
|
||
export async function GET() { | ||
try { | ||
const { account } = await ClientAW(true); | ||
const listSessions = await account.listSessions(); | ||
return successHandler(listSessions, "Sessions fetched successfully", 200); | ||
} catch (e) { | ||
return errorHandler(e); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from "zod"; | ||
|
||
export const recoveryPasswordSchema = z.object({ | ||
email: z | ||
.string({ | ||
required_error: "Email is required", | ||
invalid_type_error: "Email must be a string", | ||
}) | ||
.email({message: "Invalid email"}), | ||
}) |
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,19 @@ | ||
import { z } from "zod"; | ||
|
||
export const recoveryPasswordConfirmSchema = z.object({ | ||
newPassword: z | ||
.string({ | ||
required_error: "New password is required", | ||
invalid_type_error: "New password must be a string", | ||
}) | ||
.min(6, {message: "New password must be at least 6 characters long"}), | ||
confirmPassword: z | ||
.string({ | ||
required_error: "Confirm password is required", | ||
invalid_type_error: "Confirm password must be a string", | ||
}) | ||
.min(6, {message: "Confirm password must be at least 6 characters long"}), | ||
}).refine(data => data.newPassword === data.confirmPassword, { | ||
message: "New password and confirm password must match", | ||
path: ["confirmPassword"], | ||
}); |