Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user data server routes #41

Merged
merged 32 commits into from
Aug 18, 2023

Conversation

AasmundN
Copy link
Contributor

@AasmundN AasmundN commented Aug 14, 2023

Changes

  • user server middleware. Fetches the users data from the db and merges it with the decodedToken on the h3 event context. The user generated user object is placed on the event context.
  • Server plugin auth.server.ts to populate authStore on initial server render. Runs after the auth and user server middlewares and only on the initial server render. The plugin has access to both the event context and the nuxtApp instance, which means it can populate the Pinia store. The global auth route middleware has been updated accordingly.
  • Add db export from firebase util.
  • hasAccessLevel server util to check a users accessLevel.

API endoints

POST /api/user endpoint creates or updates a user in the firebase realtime database. A user object in the database has the following format.

{
  accessLevel?: string[]
  studyProgram: string
  updated: number
}

The user entries use the firebase token subject scope decodedToken.sub as their object keys. The currently available accessLevels are 'admin'. A default user has no accesslevel array defined. The updated field holds the date of the last time the user object was updated. The endpoint accepts a request body of the following format.

{
   sub: string
   accessLevel: string[]
   studyProgram: string
}

These specify how the data of the user with uid sub is to be updated. Only admins can update other users than their own.

GET /api/user/ endpoint returns the users data object, stored on the event context (see user middleware). If the query paramter scope is present and has the value 'all', all users will be fetched. Only admins can fetch all users.

DELETE /api/user/ endpoint deletes the users data from the database. The sub field of the request body specifies which user to remove. Only admins can remove other users than their own.

Further work

This PR only adds the endpoints to modify the data in the database. The firebase auth composable needs to be updated to use these endpoints, as well as update the users in firebase auth accordingly.

Resources

  • Firebase admin SDK realtime database reference.

@AasmundN AasmundN added the feature New feature or request label Aug 14, 2023
@AasmundN AasmundN self-assigned this Aug 14, 2023
@AasmundN AasmundN linked an issue Aug 14, 2023 that may be closed by this pull request
plugins/auth.server.ts Outdated Show resolved Hide resolved
@ipeglin ipeglin changed the base branch from main to dev August 15, 2023 14:03
pages/index.vue Outdated Show resolved Hide resolved
pages/index.vue Outdated Show resolved Hide resolved
Copy link
Member

@ipeglin ipeglin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

models/User.ts Show resolved Hide resolved
pages/index.vue Outdated Show resolved Hide resolved
server/api/user/index.delete.ts Show resolved Hide resolved
server/api/user/index.delete.ts Outdated Show resolved Hide resolved
server/api/user/index.post.ts Show resolved Hide resolved
server/api/user/index.post.ts Outdated Show resolved Hide resolved
server/api/user/index.post.ts Show resolved Hide resolved
server/api/user/index.ts Show resolved Hide resolved
server/api/user/index.ts Show resolved Hide resolved
server/api/user/index.ts Show resolved Hide resolved
@AasmundN AasmundN temporarily deployed to Development August 18, 2023 10:19 — with GitHub Actions Inactive
@AasmundN AasmundN merged commit 770792c into dev Aug 18, 2023
2 checks passed
@AasmundN AasmundN deleted the feat/27-authentication-user-data-server-routes branch August 18, 2023 10:20
@AasmundN AasmundN mentioned this pull request Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Authentication - user data server routes
3 participants