Simplify React App Login

Sagara Gunathunga
Identity Beyond Borders
5 min readJul 4, 2024

Implementing a login feature in a React app involves several key steps to ensure both functionality and security. In this post, we will look at the high-level steps and available implementation options at each stage, guiding you through a series of posts written by Asgardeo team members covering various aspects related to implementing login and securing a React app. The following diagram illustrates the high-level steps and available implementation options at each stage.

Right technology for login feature

The first step is deciding on the right technology to implement the login functionality. You could craft your own Login API, which authenticates users and returns a security token, such as JWT. This requires writing your own logic to call the Login API, process the returned token, securely store the token in the browser, and protect against common vulnerabilities affecting browser-based apps. Needless to say, this would be quite an adventurous journey. If you have enough time, expertise in security aspects, and enjoy the challenges ahead, this could be a perfect fit for you. There are production apps using this approach that work perfectly without any security, performance, or user experience issues. However, you should consider whether the effort required to implement, secure, and maintain your own Login API is worth it compared to your business expectations.

The alternative approach would be to use an open identity standard such as OpenID Connect (OIDC) to implement a login feature for your React app. The main advantages of using OIDC over a DIY (Do It Yourself) Login API are significant: OIDC is a proven and secure technology used by production apps worldwide. It can save you both time and money by allowing you to focus on your core business capabilities rather than the login features. OIDC is a mature technology, and most mainstream programming languages and frameworks provide built-in support for it. In fact, if you have basic knowledge of making HTTP calls and processing JSON messages, integrating OIDC into your React app wouldn’t be difficult.

On the flip side, you need to learn a bit about OAuth2 and OIDC technologies to adopt this approach. However, if you are a developer, your investment in learning OAuth2 and OIDC will not be wasted, and there are plenty of learning materials readily available to help you. You can run your own OIDC service using a commercial or open-source OIDC provider, such as WSO2 Identity Server, or you can use a cloud-based identity offering like Asgardeo. Most cloud identity providers offer free tiers and predictable usage-based pricing models, making it a cost-effective and efficient solution for implementing login functionality in your React app

Implementing the Login

Once you decide on the right technology that works best for you, the next step is to implement login and logout links, along with the logic to process and securely store security tokens. At this stage, you also have the freedom to write these features yourself. Although it would require time and intense effort, it would be a great experience. Similar to the previous step, the DIY approach involves handling security hardening and maintenance on your own. This can be a perfectly viable option if you are up to the challenge. Additionally, if your programming language or underlying framework comes with built-in OIDC support, you should absolutely stick with that approach. However, in the case of React, there is no built-in library or de facto framework to handle user login. In fact, one could argue that this is beyond the scope of React.

Nevertheless, instead of the DIY approach, you can look for a React Auth SDK that saves time and effort while providing protection against vulnerabilities. Additionally, you don’t need to worry about maintaining the SDK. Multiple SDKs are available that simplify integrating OIDC into your developer workflow. These SDKs handle making login and logout requests, validating and processing tokens to construct the user context, securely storing tokens, and protecting against vulnerabilities by applying best practices such as enforcing PKCE.

The Asgardeo React SDK is designed to fit seamlessly with the React development workflow, adhering to secure-by-design and secure-by-default principles. Licensed under the business-friendly Apache 2.0 open-source license, it is continually improved by the Asgardeo team and provides all the tools necessary to integrate an OIDC-based identity provider with your React app. Asgardeo team member Savindi has written an excellent post on how to integrate the React SDK with your React app. She starts with creating a hello world React application and gradually covers all topics, including adding login and logout links, configuring the OIDC provider, processing tokens, and securely calling an external API using an access token.

Read: Simplify React App Login with Asgardeo

If you’re interested in learning about the modular design, testability, and customizations of the Asgardeo React SDK, you can read this post by Brion, a core developer of the Asgardeo SDK. One of the key features highlighted in Brion’s post is the vendor-agnostic design of the Asgardeo React SDK, which allows you to seamlessly connect with any cloud or on-premise OIDC identity provider.

Read: The Foundation of Secure Authentication

React app security

If you’ve chosen the DIY path, it’s crucial to have a thorough understanding of vulnerabilities associated with browser-based apps and how to implement protections against them. As we discussed, SDKs like the Asgardeo React SDK offer essential measures to mitigate vulnerabilities specific to browser-based applications. However, it’s important to have a solid grasp of these vulnerabilities and their potential impact on your application to effectively utilize the SDK’s features. Nipuni, a member of the Asgardeo team, has written an interesting post that discusses vulnerabilities in React apps and how to implement protections against them. This post serves as an excellent reference for those interested in exploring vulnerabilities specific to browser-based applications and their mitigation.

Read: Securing Your React App to Combat the Common Threats

In this post we looked into three key steps for implementing a login feature in a React app. Firstly, we discussed the critical decision of choosing the appropriate technology, comparing the DIY approach with using OpenID Connect (OIDC). Then emphasized the advantages of OIDC in terms of security, scalability, and ease of integration. Secondly, we covered the implementation phase, including login, logout, and token processing, highlighting the challenges and benefits of each approach. Lastly, we explored the importance of securing a React app, offering insights into vulnerabilities and the use of Asgardeo React SDK to mitigate risks effectively.

The blog series discussed in this post:

  1. Simplify React App Login with Asgardeo
  2. The Foundation of Secure Authentication
  3. Securing Your React App to Combat the Common Threats

--

--

Sagara Gunathunga
Identity Beyond Borders

Director — Solutions Architecture WSO2 ANZ. Integration and Identity Architect. PMC Member @ The Apache Software Foundation