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

Strapi internal email-password login method #39

Open
lifehome opened this issue Sep 30, 2020 · 4 comments
Open

Strapi internal email-password login method #39

lifehome opened this issue Sep 30, 2020 · 4 comments

Comments

@lifehome
Copy link

Hi,

First of all, huge thanks to @ghoshnirmalya for the boilerplate, really appreciate it!
It is however, I plan to use the default "Users" collection sits inside Strapi, with the "Email" provider.

Does anyone succeed with such a combination? I am currently trying to implement a "Credentials" provider from NextAuth, but sadly despite I stuff the user object with user id, email and even the JWT token returned from /auth/local at Strapi side, I am still confused how to use such method to call methods from Strapi, e.g. authenticated GraphQL queries.

Here is my ugly code referencing the boilerplate, still debugging, so it might not work:

Providers.Credentials({
  id: 'email',
  name: 'Email', // The name to display on the sign in form (e.g. 'Sign in with...')

  // The credentials is used to generate a suitable form on the sign in page.
  // You can specify whatever fields you are expecting to be submitted.
  // e.g. domain, username, password, 2FA token, etc.
  credentials: {
    email: { label: "Email", type: "text", placeholder: "[email protected]" },
    password: {  label: "Password", type: "password" }
  },

  authorize: async (credentials) => {
    const response = await fetch(
      `${process.env.STRAPI_APIENDPOINT}/auth/local`, {
        body: JSON.stringify({
          identifier: credentials.email,
          password: credentials.password
        }),
        headers: {
          'content-type': 'application/json'
        },
        method: "POST"
      }
    )

    const data = await response.json();

    if(typeof data.user.id !== 'undefined') {
      const user = {
        id: data.user.id,
        name: data.user.username,
        email: data.user.email,
        strapi_jwt: data.jwt
      }

      return Promise.resolve(user)
    } else {
      return Promise.resolve(null)
    }
  }
})

Regards,
Ivan

@lenybernard
Copy link

Hey @lifehome, I'd be interested by this too, did you figured out ?

@ghost
Copy link

ghost commented Nov 25, 2020

@lenybernard
I have managed to implement authorized graphql queries from both google oauth and email provider. You have to pass the token from the jwt callback so that it's accessible in the session callback, where you can then add it as a property to the sesssion object. You can then access the token using getSession(); To authorize the queries to strapi you pass the token in the authorization header of the request.
header: { Authorization: "Bearer ${token}" }

@ghoshnirmalya
Copy link
Owner

@NuggetOfOdium Would you be interested in creating a pull request with the changes that you suggested?

@ghost
Copy link

ghost commented Feb 4, 2021

@ghoshnirmalya Sure thing, ill look into it on the weekend

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

No branches or pull requests

3 participants