Fauna Auth
A library for user authentication backed by FaunaDB
Installation
# npm npm i --save fauna-auth #yarn yarn add fauna-auth
Setup
There are a few preliminary steps.
- Create an account at fauna.com
- Create a database
- Inside the database you just made, create two collections:
users
andtokens
. - Create three indexes.
- One called "username" for the
users
collection with "data.username" as the term. - One called "tokens" for the
tokens
collection with "data.refreshTokens" as the term. - One called "byId" for the
tokens
collection with "data.userId" as the term.
- One called "username" for the
- Create a new key for your database.
- Create two secrets (long, preferably random strings) for signing jwt tokens.
- One for access tokens.
- Another for refresh tokens.
Note: Please do not commit your key to github. All the examples below will be using environment variables.
Now that you have your key and your secrets, you can initialize the library.
const initAuth = const FaunaAuth =
And finally, you can manage and authenticate users.
// Create a userconst newUser = await FaunaAuth // Update a userconst updatedUser = await FaunaAuth // Change a user's passwordconst updatedUser = await FaunaAuth // Delete a userawait FaunaAuth // Authenticate a userconst user = await FaunaAuth // Create tokensconst accessToken refreshToken = await FaunaAuth // Verify access tokensconst verifiedUser = await FaunaAuth // Refresh tokensconst accessToken refreshToken = await FaunaAuth // Delete refresh tokens when the user manually logs outawait FaunaAuth // Orawait FaunaAuth
Todo
- Social Authentication