-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create user, check if user already exists
- Loading branch information
Showing
18 changed files
with
211 additions
and
83 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 |
---|---|---|
|
@@ -33,3 +33,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
mongodb-realm | ||
next.config.js |
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 |
---|---|---|
@@ -1,14 +1,88 @@ | ||
export default async function signup_() { | ||
// NextResponse.json({ | ||
// isError: true, | ||
// msg: "Sorry you are not authenticated", | ||
// }); | ||
import { zfd } from "zod-form-data"; | ||
import { realmApp } from "../lib/db/realm"; | ||
import { translate } from "utils/intl"; | ||
import { WORDS } from "utils/intl/data/constants"; | ||
import { dbClient } from "../lib/db"; | ||
import { dbCollections } from "../lib/db/collections"; | ||
|
||
const response = { isError: false,msg:'it worked again' }; | ||
export default async function signup_(request: Request) { | ||
const schema = zfd.formData({ | ||
email: zfd.text(), | ||
password: zfd.text(), | ||
}); | ||
const formBody = await request.json(); | ||
|
||
const { email, password } = schema.parse(formBody); | ||
|
||
return new Response(JSON.stringify(response), { | ||
status: 200, | ||
try { | ||
const db = await dbClient(); | ||
|
||
}); | ||
if (!db) { | ||
console.log("signup_:error", "db error", db); | ||
|
||
const response = { | ||
isError: true, | ||
msg: translate(WORDS.unknown_error), | ||
}; | ||
return new Response(JSON.stringify(response), { | ||
status: 200, | ||
}); | ||
} | ||
|
||
const user = await db.collection(dbCollections.users.name).findOne({ | ||
email, | ||
}); | ||
|
||
if (user) { | ||
const response = { | ||
isError: true, | ||
msg: translate(WORDS.user_exist), | ||
}; | ||
return new Response(JSON.stringify(response), { | ||
status: 200, | ||
}); | ||
} | ||
|
||
const resp = await realmApp.emailPasswordAuth.registerUser({ | ||
email, | ||
password, | ||
}); | ||
|
||
//TODO: verify schema | ||
await db.collection(dbCollections.users.name).insertOne({ | ||
email, | ||
is_verified: false, | ||
created_at: new Date(), | ||
}); | ||
|
||
const response = { | ||
isError: false, | ||
msg: translate(WORDS.user_created), | ||
email, | ||
}; | ||
|
||
return new Response(JSON.stringify(response), { | ||
status: 200, | ||
}); | ||
} catch (error) { | ||
console.log("signup_:error", error); | ||
const errorCodeIndex = `${JSON.stringify(error)}`.lastIndexOf("code"); | ||
|
||
const code = | ||
errorCodeIndex === -1 | ||
? 0 | ||
: Number(`${error}`.substring(errorCodeIndex).match(/\d+/g)); | ||
|
||
const resp = { | ||
isError: true, | ||
msg: | ||
code === 4348 | ||
? translate(WORDS.email_error, true) | ||
: translate(WORDS.unknown_error), | ||
}; | ||
|
||
return new Response(JSON.stringify(resp), { | ||
status: 200, | ||
}); | ||
} | ||
} |
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,8 @@ | ||
import { TdbCollection } from "./types"; | ||
|
||
export const dbCollections: TdbCollection = { | ||
users: { | ||
name: "users", | ||
label: "Users", | ||
}, | ||
}; |
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,5 @@ | ||
import Realm from "realm"; | ||
|
||
export const realmApp = new Realm.App({ | ||
id: process.env.REALM_APP_ID as string, | ||
}); |
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,6 @@ | ||
export type TdbCollection = { | ||
[key: string]: { | ||
name: string; | ||
label: string; | ||
}; | ||
}; |
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
Oops, something went wrong.