{"id":2013,"date":"2024-09-20T12:00:00","date_gmt":"2024-09-20T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/react-springboot-authentication-part-2-user-registration-flow\/"},"modified":"2026-07-05T03:27:04","modified_gmt":"2026-07-05T01:27:04","slug":"react-springboot-authentication-part-2-user-registration-flow","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/react-springboot-authentication-part-2-user-registration-flow\/","title":{"rendered":"React SpringBoot Authentication \u2013 Part 2 (User Registration Flow)"},"content":{"rendered":"<p>This is Part 2 of the tutorial on How to a authenticate from React application to Spring Boot API.<\/p>\n<p>In this part you will actually implement user authentication in Spring Boot. Then you will see how to store user credentials in in React using local storage (session or cookies).<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Add the LoginRequest Model and Update the User Model<\/a><\/li>\n<li><a href=\"#t2\">Write the Authenticate Service Method<\/a><\/li>\n<li><a href=\"#t3\">Write the Login Controller Method<\/a><\/li>\n<li><a href=\"#t4\">Implement the Registration and RegistrationSuccessful Components<\/a><\/li>\n<li><a href=\"#t5\">Implement the Login and LoginSuccessful Components<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Add the LoginRequest Model and update the User model<\/strong><\/h4>\n<p>The LoginRequest would be used to hold the username and password coming from the request body of a login request. I would just have fields:<\/p>\n<ul>\n<li>id<\/li>\n<li>username<\/li>\n<li>password<\/li>\n<\/ul>\n<p>We would also add the passwordHash field to the user model and this is going to be a string field<\/p>\n<p>Update the addUser method of the UserService so that when a new user is being saved, the passwordHash field is also set.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Write the Authenticate Service Method<\/strong><\/h4>\n<p>Write the authenticate method in the UserService. This method would take a LoginRequest object (username and password) and checks if the values are valid<\/p>\n<p>The authenticate method placed in the UserService and is given below:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">boolean<\/span> <span style=\"color: #0066bb; font-weight: bold;\">authenticate<\/span><span style=\"color: #333333;\">(<\/span>String username<span style=\"color: #333333;\">,<\/span> String password<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    User user <span style=\"color: #333333;\">=<\/span> userRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findByUsername<\/span><span style=\"color: #333333;\">(<\/span>username<span style=\"color: #333333;\">);<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(!<\/span>user<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getUsername<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>username<span style=\"color: #333333;\">)){<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">throw<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">UsernameNotFoundException<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"User not found in the database\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getPasswordHash<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">equals<\/span><span style=\"color: #333333;\">(<\/span>bCryptPasswordEncoder<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">encode<\/span><span style=\"color: #333333;\">(<\/span>password<span style=\"color: #333333;\">)))<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">throw<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">BadCredentialsException<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"The password is incorrect\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Write the Login Controller Method<\/strong><\/h4>\n<p>You will write the controller method for <em>\/login<\/em>.<\/p>\n<p>This method would receive the LoginRequest in the request body and call the authenticate method. At successful authentication it returns an Ok status code<\/p>\n<p>The <em>login()<\/em> method is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/login\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> ResponseEntity<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">login<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestBody<\/span> LoginRequest loginRequest<span style=\"color: #333333;\">,<\/span> HttpSession session<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">try<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">boolean<\/span> isAuthenticated <span style=\"color: #333333;\">=<\/span> authenticationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">authenticate<\/span><span style=\"color: #333333;\">(<\/span>loginRequest<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getUsername<\/span><span style=\"color: #333333;\">(),<\/span> loginRequest<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getPassword<\/span><span style=\"color: #333333;\">());<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">if<\/span> <span style=\"color: #333333;\">(<\/span>isAuthenticated<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n            session<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setAttribute<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"user\"<\/span><span style=\"color: #333333;\">,<\/span> loginRequest<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getUsername<\/span><span style=\"color: #333333;\">());<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">return<\/span> ResponseEntity<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">ok<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Login successful\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n        <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">else<\/span> <span style=\"color: #333333;\">{<\/span>\r\n            <span style=\"color: #008800; font-weight: bold;\">return<\/span> ResponseEntity<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">status<\/span><span style=\"color: #333333;\">(<\/span>HttpStatus<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">UNAUTHORIZED<\/span><span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">body<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Invalid username or password\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n        <span style=\"color: #333333;\">}<\/span>\r\n    <span style=\"color: #333333;\">}<\/span> <span style=\"color: #008800; font-weight: bold;\">catch<\/span> <span style=\"color: #333333;\">(<\/span>Exception e<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">return<\/span> ResponseEntity<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">status<\/span><span style=\"color: #333333;\">(<\/span>HttpStatus<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">INTERNAL_SERVER_ERROR<\/span><span style=\"color: #333333;\">).<\/span><span style=\"color: #0000cc;\">body<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"An error occurred during login\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n    <span style=\"color: #333333;\">}<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Note that this method has HttpSession as its second parameter but you don&#8217;t need to worry about it as Spring would handle creating and managing sessions.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Implement the Registration and RegistrationSuccessful Components<\/strong><\/h4>\n<p>Create a Registration Component that would contain\u00a0 the following fields:<\/p>\n<ul>\n<li>firstname<\/li>\n<li>lastname<\/li>\n<li>username<\/li>\n<li>password<\/li>\n<li>confirmPassword<\/li>\n<\/ul>\n<p>Get the <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/product-app-react-ui\/refs\/heads\/master\/registration-form.txt\" target=\"_blank\" rel=\"noopener\">designed form here<\/a>.<\/p>\n<p>The markup and script for the R<span style=\"font-family: 'Source Sans Pro', Graphik, -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif; font-size: 1.125rem;\">egistrationSuccessful component is given below:<\/span><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">import React from 'react'\r\nimport {Typography, Container, Box } from '@mui\/material';\r\n\r\nexport default function RegistrationSuccess() {\r\n  return (\r\n    <span style=\"color: #007700;\">&lt;Container<\/span> <span style=\"color: #0000cc;\">maxWidth=<\/span><span style=\"background-color: #fff0f0;\">\"xs\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;Box<\/span> <span style=\"color: #0000cc;\">sx=<\/span><span style=\"background-color: #fff0f0;\">{{<\/span> <span style=\"color: #0000cc;\">mt:<\/span> <span style=\"color: #0000cc;\">8<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">,<\/span> <span style=\"color: #0000cc;\">color:<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">'<\/span><span style=\"color: #0000cc;\">green<\/span><span style=\"color: #ff0000; background-color: #ffaaaa;\">'<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">}}<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;Typography<\/span> <span style=\"color: #0000cc;\">variant=<\/span><span style=\"background-color: #fff0f0;\">\"h3\"<\/span> <span style=\"color: #0000cc;\">align=<\/span><span style=\"background-color: #fff0f0;\">\"center\"<\/span> <span style=\"color: #0000cc;\">gutterBottom<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n        You have successfully Registered. You can now Login\r\n      <span style=\"color: #007700;\">&lt;\/Typography&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;Typography<\/span> <span style=\"color: #0000cc;\">variant=<\/span><span style=\"background-color: #fff0f0;\">\"h6\"<\/span> <span style=\"color: #0000cc;\">align=<\/span><span style=\"background-color: #fff0f0;\">\"center\"<\/span> <span style=\"color: #0000cc;\">gutterBottom<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n        <span style=\"color: #007700;\">&lt;a<\/span> <span style=\"color: #0000cc;\">href=<\/span><span style=\"background-color: #fff0f0;\">\"#\"<\/span><span style=\"color: #007700;\">&gt;<\/span>You can now Login<span style=\"color: #007700;\">&lt;\/a&gt;<\/span>\r\n      <span style=\"color: #007700;\">&lt;\/Typography&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;\/Box&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;\/Container&gt;<\/span>\r\n\r\n  )\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Create the Login Component<\/strong><\/h4>\n<p>The Login component would contain 2 fields, username and password.<\/p>\n<p>When the user fills and clicks on the Submit, it makes a post request to the \/login endpoint and a response of either 200(Ok) or exception is returned.<\/p>\n<p>At successful login, the user should be directed to the LoginSuccess component. You also need to create this component<\/p>\n<p>Get the <a href=\"https:\/\/raw.githubusercontent.com\/KindsonTheGenius\/product-app-react-ui\/refs\/heads\/master\/login-form.txt\" target=\"_blank\" rel=\"noopener\">markup for the Login component here<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is Part 2 of the tutorial on How to a authenticate from React application to Spring Boot API. In this part you will actually &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-2013","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2013"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2013\/revisions"}],"predecessor-version":[{"id":2181,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2013\/revisions\/2181"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}