-
Notifications
You must be signed in to change notification settings - Fork 197
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
1 parent
a7385e3
commit 3449e19
Showing
45 changed files
with
1,472 additions
and
452 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
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,36 +1,10 @@ | ||
import { UserEntity } from '../../db/entities'; | ||
import { v4 } from 'uuid'; | ||
import { | ||
getUserByUsername, | ||
getOrSaveUser, | ||
updateUserPassword, | ||
} from '../../db/actions/users'; | ||
import { hashPassword } from '../../utils'; | ||
import { compare } from 'bcryptjs'; | ||
import { UserIdentityEntity } from '../../db/entities'; | ||
import { registerAnonymousUser } from '../../db/actions/users'; | ||
|
||
export default async function loginAnonymous( | ||
username: string, | ||
password: string | ||
): Promise<UserEntity | null> { | ||
const actualUsername = username.split('^')[0]; | ||
const existingUser = await getUserByUsername(username); | ||
if (!existingUser) { | ||
const hashedPassword = await hashPassword(password); | ||
const user = new UserEntity(v4(), actualUsername, hashedPassword); | ||
user.username = username; | ||
user.language = 'en'; | ||
|
||
const dbUser = await getOrSaveUser(user); | ||
return dbUser; | ||
} | ||
|
||
if (!existingUser.password) { | ||
const hashedPassword = await hashPassword(password); | ||
const dbUser = await updateUserPassword(existingUser.id, hashedPassword); | ||
return dbUser; | ||
} | ||
|
||
const isPasswordCorrect = await compare(password, existingUser.password); | ||
|
||
return isPasswordCorrect ? existingUser : null; | ||
): Promise<UserIdentityEntity | null> { | ||
const identity = registerAnonymousUser(username, password); | ||
return identity; | ||
} |
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.