15
34 Comments

I don't know what to do regarding Auth. Please help!

Hi all,

I need to create a ReactJS frontend and a React Native mobile app.
Those two apps will call my C# Web API.
So currently I'm really struggling on which authentication/authorization solution to decide.

I had a look a Auth0, but it seems pricey an difficult to integrate with a custom database. Also I do not really understand how it would fit in into my architecture.

What I wanted to do is to code it on my own. But I don't know if it's "enough" secure when I use SSL and JSON Web Tokens. But here the trade off would be, to code everything on my own.

What do you guys think? Can you give me some tips?

on November 16, 2020
  1. 4

    Firebase Auth is super easy to implement. You should try that before rolling your own.
    https://firebase.google.com/docs/auth

    1. 1

      But how do I then connect this users with my custom db?

      1. 1

        The whole authorization will take place in the frontend (user logs in with his google account, gives permission to your app, yada yada). In the end, you will receive an token from the "Firebase Frontend SDK". Then you make a call to your own backend together with the token and then use a "Firebase Backend SDK" to verify the token. Once it is verified, you can take a look at the contents of the token, and viola, there is a user id! It will be something like "google:123348436" or "github:138523". Use this id in your custom database.

      2. 1

        Firebase has an SDK for .NET Platform , Just verify the Token in the backend which will provide the user id. Save the user in your database against this ID

        1. 1

          Thanks, I will create a prototype and try it :)

  2. 3

    Hello! DM me and I’ll send you my NetcoreSaas code regarding authentication, basically it’s JWT with email and password on NET Core, and Postmark for password reset and account confirmation.

    1. 1

      Hey, thanks a lot. Sent you a mail.

      1. 1

        really have to recommend against doing this yourself. It's rife with security vulnerabilities and just gets more and more complicated. Here's a related article.

  3. 1

    Did you crack this? Would be interested to know your experience as am looking to provide a solution that would make this super easy.

    1. 1

      I don't use any provider and will implement auth by myself. Will consider pen tester and security consultant when app has traction.

  4. 1

    We're using Keycloak on our open source projects to secure our frontend, mobile, and REST APIs and so far it accommodates all our requirements. It has a good documentation and libraries as well like KeycloakAdmin. You can also check Cognito from Amazon, it's pretty stable and easy to integrate as well.

  5. 1

    I prefer to build it on my own, because then I know how to integrate it and can do everything I need.
    Fore some projects I hosted my own https://www.keycloak.org/. Then you don't need to implement it and only pay for the provisioning costs.

  6. 1

    May I know your backend?

    1. 2

      What do you mean exactly?

      1. 1

        Oh wait, I didn't see that you were using C# Web API.
        I usually work with PHP backend, and how I handle it usually by validating the input first
        say you authenticate with email and password. I will create a function to validate:

        1. if email and password exists in the request
        2. if email and password is not empty
        3. if email is valid email
        4. if email is exists in database
        5. get the data from database
        6. check if password from database is matched with password from user input
          validation passed. now run function to create token/just let the user login

        is this the type of answer you're looking for?

        (By the way, just yesterday, I created screencast about login, i think it may help you. It's written in PHP, though, but the concept is the same. https://gum.co/funphp )

        1. 1

          Thanks for the explanation, but I wasn't looking for this kind of answer :D
          I was just asking, if it's secure enough to handle authentication and authorization with JWT. Of course with ssl also.

          Copied from other answer:
          Often I see in some solutions, that there is an auth server and a resource server (like this: https://bitoftech.net/wp-content/uploads/2014/09/OAuthRoles.jpg)
          Do I need to split issuing tokens and returning resources in two servers or can I use the same server for the two things?

          1. 1

            Ah okay then, glad you've found the answer 👍🏻

            1. 1

              Well it's not answered yet^^ I don't know if I can use the same API/Server to issue tokens and return requested resources.

              1. 1

                If I were in your position, I would do that, using the same server to do both.

                1. 1

                  Thanks a lot!

  7. 1

    There is also AuthGuardian from OpenGraph that you could look at.
    https://www.onegraph.com/auth-guardian/

  8. 1

    Look at our SaaS boilerplate. The backend is done with python, but the front-end - with the React. You will able to get the idea and code samples to reimplement on your own.

    https://github.com/saasforge/open-source-saas-boilerpate

    1. 1

      Thanks, just a question regarding JWT:
      Often I see in some solutions, that there is an auth server and a resource server (like this: https://bitoftech.net/wp-content/uploads/2014/09/OAuthRoles.jpg)
      Do I need to split issuing tokens and returning resources in two servers or can I use the same server for the two things?

      1. 1

        No, there is no need to use different servers for it. I investigated this issue pretty deeply and in most cases, there is no need to use anything like that. You still can implement 2-servers architecture but it's up to you, no necessity here.

  9. 1

    But I don't know if it's "enough" secure when I use SSL and JSON Web Tokens.

    I've built my own auth 10+ times theres libraries that make it easier, it can be a hassle to setup the oauth apps with social providers and security wise you need to make sure to hash + salt passwords in the DB but JWT + SSL is plenty secure

    1. 1

      Thanks for the answer :) I don't need social login.

      Copied from above:

      Often I see in some solutions, that there is an auth server and a resource server (like this: https://bitoftech.net/wp-content/uploads/2014/09/OAuthRoles.jpg)
      Do I need to split issuing tokens and returning resources in two servers or can I use the same server for the two things?

      1. 1

        These can definitely just be different endpoints on the same server.

        You don't even need JWT.

        Client:
        /POST users with Email & Password
        Server:
        creates a DB entry with hashed and salted password, generate and save to DB an auth token which is essentially just a random string of characters (for example use bcrypt hex). Server sets a server only secure cookie with the auth token value

        For each future request check whether the request has an authorization cookie and if so use that cookie to get the user that made the request.

        Logout is a request to the server that clears the cookie.
        Being logged in is a successful fetch the current user from an endpoint.
        Login is giving an email and password, the server checking the salted and hashed sent password matches the DB entry and then sets the auth token cookie

        1. 1

          Thanks a lot David
          You helped me out :)

  10. 1

    If you are trying to build a webapp and a native app at the same time, you are most definitely better off going with something you can just plug in and forget, at least for now. Auth0 will cost you $20/m if you don't have a lot of users. If you have a lot of users, deal with your next problem next.

    There is no sense to build anything on your own unless it is somehow giving you a massive advantage, or you are so successful that you want to start rolling 3rd party soft into your own platform such that your users become dependent on you, over that other software.

    Done is better than perfect.

    1. 1

      True, but I can't think how I'll connect my users in my Auth0 app, to my db.
      For example: There is a owner of a business and he has a role and account stored in Auth0. So if I register a business in my db, what is the value of the foreign key which references the user (the business owner)?

      1. 1

        You would grab the UUID of the user in Auth0 and assign that value to your business in the db. This is how I do it for one of my projects where I don't need a robust relationship between users and data. (SmashNotes.com)

        In another project, I experimented with a "local user," and every time a new user logs, I just create a new account for them:User.create(auth_id: [auth0_uuid_param]). Then, I let Auth0 take care of all the hard stuff, and use this local user only for setting local account permissions, stuff like your payments plan, how many credits you have. (ClipGain.io)

        ps. Auth0 has a permissions and access API, but it's a bit more involved and for most basic things I just use a local user account. Look up their "Management API" if you are curious.

      2. 1

        yeah auth0 doesn't well support permissioning and access control, so thats problem there, however that's something that authorization SaaS like Authress solve.

    2. 1

      Auth0 is free if you have less then 7000 active users, which is different than registered users, active users have to at least login or register during the month. It might seem expensive but you are outsourcing the responsibility of storing users details which can be hacked and stolen if you do this yourself.

  11. 1

    Are you trying to get a product out quickly? If so, use auth0.

    Are you trying to learn? Roll your own code, so long as you don't mind the possibility of it being insecure.

    Or, try to find a battle tested library.