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

[FEATURE REQUEST]: Hooks/callbacks on sxx server routes #92

Closed
1 task done
iankressin opened this issue Feb 24, 2023 · 2 comments
Closed
1 task done

[FEATURE REQUEST]: Hooks/callbacks on sxx server routes #92

iankressin opened this issue Feb 24, 2023 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@iankressin
Copy link

iankressin commented Feb 24, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Feature

Currently there's no way to execute arbitrary code during the authentication process on the server.

This could be very handy for applications that use some kind of SQL database and needs to create a record their users, so they can later create relationships between the user and the rest of models on application.

How would it work?

One way to handle the execution of user defined functions on the server, would be adding a type to SSXServerRoutes union types such as:

export interface SSXServerRoutes {
   //...
    login?: Partial<SSXRouteConfig> | 
        AxiosRequestConfig |
        string |
        { path: string, hook: (req: Request) => Promise<void> };
  //...
}

hook must receive a Request as parameters, so the function can check internally if the user is authenticated and decide which path to take. It'll return a Promise<void> in order to enable async hooks, but to make sure the function doesn't return anything, since it wouldn't be used by ssx during the authentication.

the hook would be called right before the response to the client, to make sure it doesn't interfere on the other stuff the route is doing.

Should the whole route fail if hook throws?

I'd say yes, otherwise the client wouldn't know that something went wrong during the login, for example

Should the hook be executed if authentication fails, in the case of /ssx-login route?

Yes, if the function shouldn't be executed when the auth fails, req.ssx.verified should be used decide whether to continue or not.

Usage example

app.use(
  SSXExpressMiddleware(
      ssx,
      {
          login: {
              route: '/login',
              hook: async (request: Request) => {
                  if (!req.ssx.verified)
                      return

                  const user = await userService.get(req.ssx.siwe.address)
                  
                   if (!user)
                       await userService.create(req.ssx.siwe.address)
              }
          },
      }
    )
)

User Stories

As a developers I'd like to be able to create a record on the database if it's the first time a user is signing in on my app

@obstropolos
Copy link
Contributor

#105 in progress

@obstropolos
Copy link
Contributor

Updates to this were included in ssx-server-middleware/v1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants