Skip to content

Instantly share code, notes, and snippets.

@nprail
Last active June 5, 2020 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nprail/16a6fc0839e8313098540f5208de2db3 to your computer and use it in GitHub Desktop.
Save nprail/16a6fc0839e8313098540f5208de2db3 to your computer and use it in GitHub Desktop.
Super basic authentication example
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