Implementation
Native Auth
Implement native username/password authentication
- In your
src
directory, create ahooks.server.ts
file
- Create a PostgreSQL table with the following columns:
- In your
routes
directory, create a route calledlogin
. In your login+page.svelte
, create form elements for taking in username and password inputs. Create buttons for login and register.- Ensure the endpoints are set to
registerValidate
andloginValidate
- Create a separate button that is set to
logout
endpoint
- Ensure the endpoints are set to
- In your
.env
file (create if you haven’t done so) that takes in your database URI and user table name
-
After submitting the form, the user will be redirected to the endpoint of your choice.
-
Upon registering, the user will be added to the database with the username and a secure hashed password.
-
On login, the user will be authenticated through your database.
-
A browser cookie will be created as well as a session in local memory storage called “svault-auth”.
- The session will have an expiration time determined in your
.env
file. - Sessions will automatically be cleaned and deleted upon expiration.
- On logout, the user will be redirected to the home page, the cookie will be deleted from the browser, and the session will be deleted from local memory store.
-
And you’re good to go!