next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

Question and Enhancement for using: graphql, jwt token, updateSession

Open CRGavrila opened this issue 6 years ago • 2 comments

Your question

Q1: Whouldn't be great instead of function NextAuth(req,res,options) to be a React component? Use case: Because I use graphql and credential provider, I need to mutate from a react component and after mutation I must pass exact user props comming from db (not null, not undefined beacouse this second argument is stingifyed) signIn('credentials', { ...userData })

Q2: How to access jwt token from backend? Description: I have docker for frontend and one docker for backend. I've installed next-auth on both server and backend. In backend getSession is not working beacuse React must be installed, and I don't get nothing from getToken() ,options are: jwt: { secret: process.env.SECRET, encryption: true, }, session: { jwt: true, },

Q3: can be a function called updateSession where I can update session? Description: I have profile page and altered User model and added: firstName, lastName, phoneNumber. When I update profile (for example phoneNumber) I would like update session to persist updated user (even from refresh)

[ *] Found the documentation helpful [ ] Found documentation but was incomplete [ ] Could not find relevant documentation [ *] Found the example project helpful [ ] Did not find the example project helpful

CRGavrila avatar Nov 04 '20 22:11 CRGavrila

Hi!

Q1. Could you elaborate more on this? I don't think I quite understand what you are asking.

Q2. To get hold of the JWT token, you can use getToken() the following way:

// eg. /api/endpoint-using-jwt

import jwt from "next-auth/jwt"

export default function(req, res) {
	const token = jwt.getToken({req, secret: process.env.SECRET, encryption: true})
    if (token) {
      // do something with the token

      res.end()
    }

  // There was no token found, probably the user is logged out when this endpoint was invoked
  res.end()
}

From your description, it sounds like you actually have two different apps, which do not share the NextAuth instance. You cannot use getToken in the backend in that situation. I am wondering though, if you use Next.js, why do you have the need for a separate backend and frontend (two docker containers)? Next.js has the /api routes, that can be used to create any backend for your frontend. (In fact, you probably use that to set up next-auth in the first place). The "backend" docker container can be integrated under the same /api route (api folder in your code), next to where you probably defined [...nextauth].js

Q3. It might be opinionated, but I think the point of an Identity Provider is that it will handle your identity on behalf of your application, so it might be a better solution to just redirect the user to your IdP's "Edit Profile" page. Although it would not update the application with the new profile information, so you probably need to trigger a sign-in to update your app's profile info.

balazsorban44 avatar Nov 07 '20 11:11 balazsorban44

Thx for your replay,

R1: I was wondering if NextAuth could be a React Component, but from what I see due to architecture is a no go :) R2: getToken dosen't work. I have to put that token in header to get in backend R3. That is what I was afraid of

Thx

CRGavrila avatar Nov 11 '20 09:11 CRGavrila