is it possible to use custom authentication

default discord avatar
Bakry
9 months ago
45

s there anyway to change authentication main field from email to phone number and can i use fully custom authentication ?



@jmikrut 👀

  • discord user avatar
    jmikrut
    Payload Team
    9 months ago

    hey there - you can certainly build you own authentication patterns



    disableLocalStrategy: true


    and then you are free to use any Passport-compatible auth strategy, or you can create your own

  • default discord avatar
    Bakry
    9 months ago

    can you elaborate further to how can i do this exactly?



    and thank you very much

  • default discord avatar
    Arskeliss
    9 months ago
  • default discord avatar
    Bakry
    9 months ago

    the thing is



    im making a phone number/ password auth

  • default discord avatar
    Arskeliss
    9 months ago

    Or maybe this because this is react based

    https://github.com/ederssouza/reactjs-auth-boilerplate
  • default discord avatar
    Bakry
    9 months ago

    and i'm not sure how to do make a passport auth work with payload

  • default discord avatar
    Arskeliss
    9 months ago

    Ahm, sorry but me neither, never needed it



    But



    It's surely possible to change the default with you own component



    @Bakry

    https://github.com/payloadcms/next-auth-frontend


    Take a look at this and maybe you could rewrite it



    If you are using next as frontend



    Oh no wait, i don't know whether it will work with this

  • default discord avatar
    Bakry
    9 months ago

    i am not unfortunately



    tyty



    idk how to implement it in the backend tbh

  • default discord avatar
    Arskeliss
    9 months ago

    It needs quite of an aexperience as a developer to implement your own PassportJS auth strategies as it is stated in the docs as well, but it doesnt seem that difficult looking at PassportJS documentation to do so

  • default discord avatar
    Bakry
    9 months ago

    yea it isn't that hard to make but the thing is



    idk how to make it work with payload and payload admin

  • discord user avatar
    jmikrut
    Payload Team
    8 months ago

    you'd just inject your own components into the login screen and make sure that your middleware properly authenticates a user via an HTTP-only cookie. If authenticated, you add the user to the

    req.user

    and then you're golden!

  • default discord avatar
    Bakry
    8 months ago

    so if i make a token using jwt.sign



    then set it in a cookie using setTokenCookie



    and save my req.user as the current user

  • discord user avatar
    jmikrut
    Payload Team
    8 months ago

    yep



    that's basically all that Passport does really

  • default discord avatar
    Bakry
    8 months ago

    the payload middleware authenticates my jwt automatically ?

  • discord user avatar
    jmikrut
    Payload Team
    8 months ago

    it's just middleware that looks out for a cookie or a JWT header, and then decodes the JWT, and sets it to

    req.user


    No, if you are using a custom JWT, then Payload will not know to use it. It will be up to you to do that

  • default discord avatar
    Bakry
    8 months ago

    what if i hook it up with ${payload.config.cookiePrefix}



    as the token name

  • discord user avatar
    jmikrut
    Payload Team
    8 months ago

    you will still need to make sure you use the same encryption that the passport-local-mongoose package uses



    but it could be done

  • default discord avatar
    Bakry
    8 months ago

    hmm can you elaborate more



    yours truly is new to nodejs



    aight i got some ideas i will try to implement and let you know if i run into any problems



    I copied the strategy from the official payload repo



    and in /login i made my own jwt using payload.secret



    and set the cookie with payload.config.cookiePrefix-token



    and set the req.user to the data i got



    and its working somehow lmao

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.