mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: pick username
* put new registered social login user to session data * ask to choose username * simplify checkUsername functions
This commit is contained in:
1 parent
5591689816
commit
182fcf643f
12 files changed
+244
-69
No files matched your search
@@ -16,6 +16,14 @@ export async function issueToken(req, res) {
|
||||
res.json({ token })
|
||||
}
|
||||
|
||||
async function invalidUsername(username) {
|
||||
return !isValidUsername(username) || User.exists({ username })
|
||||
}
|
||||
|
||||
async function invalidEmail(email) {
|
||||
return !isValidEmail(email) || User.exists({ email })
|
||||
}
|
||||
|
||||
export async function checkUsername(req, res) {
|
||||
const { username, email } = req.query
|
||||
const cred = username || email
|
||||
@@ -27,30 +35,16 @@ export async function checkUsername(req, res) {
|
||||
}
|
||||
|
||||
if (isValidEmail(cred)) {
|
||||
if (await User.exists({ email: cred })) {
|
||||
res.status(409).json({
|
||||
type: 'email',
|
||||
message: `"${cred}" already exists`,
|
||||
})
|
||||
return
|
||||
}
|
||||
res.status(200).json({
|
||||
res.json({
|
||||
type: 'email',
|
||||
message: `"${cred}" is available`,
|
||||
available: !(await User.exists({ email: cred })),
|
||||
})
|
||||
return
|
||||
}
|
||||
if (isValidUsername(cred)) {
|
||||
if (await User.exists({ username: cred })) {
|
||||
res.status(409).json({
|
||||
type: 'username',
|
||||
message: `"${cred}" already exists`,
|
||||
})
|
||||
return
|
||||
}
|
||||
res.status(200).json({
|
||||
res.json({
|
||||
type: 'username',
|
||||
message: `"${cred}" is available`,
|
||||
available: !(await User.exists({ username: cred })),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -68,18 +62,18 @@ export async function register(req, res, next) {
|
||||
return
|
||||
}
|
||||
|
||||
if (await User.exists({ username })) {
|
||||
if (await invalidUsername(username)) {
|
||||
res.status(409).json({
|
||||
type: 'username',
|
||||
message: 'username already exists',
|
||||
message: 'Invalid username',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (await User.exists({ email })) {
|
||||
if (await invalidEmail(email)) {
|
||||
res.status(409).json({
|
||||
type: 'email',
|
||||
message: 'email already exists',
|
||||
message: 'Invalid email',
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -126,6 +120,55 @@ export function logout(req, res) {
|
||||
|
||||
export function checkSession(req, res) {
|
||||
res.json({
|
||||
loggedIn: req.isAuthenticated(),
|
||||
loggedIn: req.isAuthenticated() && !req.session.newUser,
|
||||
})
|
||||
}
|
||||
|
||||
export async function socialLogin(req, res) {
|
||||
const { newUser } = req.user
|
||||
if (newUser) {
|
||||
req.session.newUser = newUser
|
||||
res.redirect('/choose-username')
|
||||
return
|
||||
}
|
||||
res.redirect('/')
|
||||
}
|
||||
|
||||
export async function registerFromSession(req, res) {
|
||||
const { username } = req.body
|
||||
const { newUser } = req.session
|
||||
|
||||
if (!newUser) {
|
||||
res.status(400).json({
|
||||
message: 'no new user found',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (await invalidUsername(username)) {
|
||||
res.status(409).json({
|
||||
type: 'username',
|
||||
message: 'Invalid username',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
newUser.username = username
|
||||
const user = new User(newUser)
|
||||
|
||||
await user.save()
|
||||
|
||||
req.login(user, (err) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
res.status(500).json({
|
||||
message: 'error logging in',
|
||||
})
|
||||
return
|
||||
}
|
||||
delete req.session.newUser
|
||||
res.status(201).json({
|
||||
message: 'user created',
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user