-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
45 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,38 +1,44 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import { getSession } from "next-auth/react"; | ||
import { getServerSession } from "next-auth/next"; | ||
import { MongoClient } from "mongodb"; | ||
import { authOptions } from "./auth/[...nextauth]"; | ||
|
||
const client = new MongoClient(process.env.MONGODB_URI || ""); | ||
|
||
export default async (req: NextApiRequest, res: NextApiResponse) => { | ||
const session = await getSession({ req }); | ||
if (!session || !req.body.username) { | ||
return res.status(400).send({ | ||
error: | ||
"You're missing to log in or to provide the username to set.", | ||
}); | ||
} | ||
const session = await getServerSession(req, res, authOptions); | ||
if (!session || !req.body.username) { | ||
return res.status(400).send({ | ||
error: "You're missing to log in or to provide the username to set.", | ||
}); | ||
} | ||
|
||
if (!req.body.username.match(/^[a-zA-Z0-9_]+$/)) { | ||
return res.status(400).send({ | ||
error: "The username use only letters, numbers and underscores.", | ||
}) | ||
} else if (req.body.username.length > 21) { | ||
return res.status(400).send({ | ||
error: "The username must be between 1 and 21 characters long.", | ||
}) | ||
} | ||
if (!req.body.username.match(/^[a-zA-Z0-9_]+$/)) { | ||
return res.status(400).send({ | ||
error: "The username use only letters, numbers and underscores.", | ||
}); | ||
} else if (req.body.username.length > 21) { | ||
return res.status(400).send({ | ||
error: "The username must be between 1 and 21 characters long.", | ||
}); | ||
} | ||
|
||
console.log(req.body); | ||
console.log(req.body); | ||
|
||
const email = session?.user?.email; | ||
await client.connect(); | ||
let db = await client.db("auth"); | ||
let doc = await db | ||
.collection("users") | ||
.findOneAndUpdate( | ||
{ email }, | ||
{ $set: { username: req.body.username, name: req.body.name, bio: req.body.bio } } | ||
); | ||
return res.status(200).send(doc); | ||
const email = session?.user?.email; | ||
await client.connect(); | ||
let db = await client.db("auth"); | ||
let doc = await db | ||
.collection("users") | ||
.findOneAndUpdate( | ||
{ email }, | ||
{ | ||
$set: { | ||
username: req.body.username, | ||
name: req.body.name, | ||
bio: req.body.bio, | ||
}, | ||
} | ||
); | ||
return res.status(200).send(doc); | ||
}; |
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