Last active
June 5, 2020 12:22
-
-
Save nprail/16a6fc0839e8313098540f5208de2db3 to your computer and use it in GitHub Desktop.
Super basic authentication example
This file contains hidden or 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
const users = [ | |
{ | |
username: 'foo', | |
password: 'bar' | |
} | |
] | |
const authenticate = credentials => { | |
if (!credentials || !credentials.username || !credentials.password) { | |
// credentials were not passed in | |
return false | |
} | |
const user = users.find(u => u.username === credentials.username) | |
if (!user) { | |
// user doesn't exist | |
return false | |
} | |
if (credentials.password !== user.password) { | |
// user exists but password didn't match | |
return false | |
} | |
// user exists and password matched | |
return true | |
} | |
// wrong password | |
authenticate({ | |
username: 'foo', | |
password: 'wrong' | |
}) | |
// false | |
// wrong username | |
authenticate({ | |
username: 'wrong', | |
password: 'bar' | |
}) | |
// false | |
//both correct | |
authenticate({ | |
username: 'foo', | |
password: 'bar' | |
}) | |
// true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment