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

Is possible mutate session with a any action in front-end? #2507

Closed
victorst79 opened this issue Aug 9, 2021 · 1 comment
Closed

Is possible mutate session with a any action in front-end? #2507

victorst79 opened this issue Aug 9, 2021 · 1 comment
Labels
duplicate This issue or pull request already exists question Ask how to do something or how something works

Comments

@victorst79
Copy link

victorst79 commented Aug 9, 2021

Question 💬

I am working on an application, in which I save the user data in the session after logging in. I use next-auth to have control over the data and the pages that a user can see or not see.

I have a custom backend which I call inside authorize to do the login. So far so good, my problem is that I have a page with information about a user, the user can change this information (email, password, dates, etc). This action affects my custom backend because the POST call that modifies this data is obviously in my backend. But I also want to mute or modify it in my session without having to close session and login again.
Is there any way to do this?

I have seen some issues in the repository but almost all of them are oriented to refresh the token and not mutate it with an action from the front-end.

Thanks!

How to reproduce ☕️

This is my [...nextauth].js file:

export default NextAuth({
  providers: [
    Providers.Credentials({
      name: 'Custom Auth',
      credentials: {
        email: {
          label: 'Email',
          type: 'text',
          placeholder: 'Email',
        },
        password: {
          label: 'Password',
          type: 'password',
          placeholder: 'Password',
        },
      },
      authorize: async ({ email, password }) => {
        try {
          const {
            data: { data },
          } = await axios.post('/login', {
            data: {
              email: email,
              password: password,
            },
          });
          if (!data) throw new Error('Data not returned!');
          return data;
        } catch (e) {
          throw new Error(e.message);
        }
      },
    }),
  ],
  callbacks: {
    jwt: async (token, user, account, profile, isNewUser) => {
      user && (token.user = user);
      return Promise.resolve(token);
    },

    session: (session, user, sessionToken) => {
      session.sessionData = user.user;
      delete session['user'];
      return Promise.resolve(session);
    },
  },
  pages: {
    signIn: '/login',
    error: '/login',
  },
});

The structure of my session is:

sessionData: {
  "token": "My-Token",
  "session_id": "Session-id",
  "user": {
    "email": "email@email.com",
    "lang": "eng",
    "name": "Name",
    "surname": "Surname",
    "timezone": "Europe/Lisbon",
  }
}

Contributing 🙌🏽

Yes, I am willing to help answer this question in a PR

@victorst79 victorst79 added the question Ask how to do something or how something works label Aug 9, 2021
@balazsorban44
Copy link
Member

duplicate of #2269

@balazsorban44 balazsorban44 added the duplicate This issue or pull request already exists label Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists question Ask how to do something or how something works
Projects
None yet
Development

No branches or pull requests

2 participants