Skip to content
Binarybox Tutorials

Binarybox Tutorials

Binarybox Tutorials provide tutorials on Web Development and Programming, Learn about Laravel, Symfony, Codeigniter, Javascript other Technologies with our easy step-by-step tutorials

  • Home
  • Laravel
  • CodeIgniter
  • Symfony
  • ReactJS
  • Vue.js
  • Angular
  • Nuxt
  • Tools
  • Contact Us
  • About
How To Develop Login And Registration in Angular 19 App using API
February 16, 2025 Angular 19

How To Develop Login And Registration in Angular 19 App using API

Avatar photoPosted by Binarybox Tutorials

Contents

  • Video Tutorial:
  • Prerequisite:
  • Step 1: Create An Angular 19 Project
  • Step 2: Install packages
  • Step 3: Configure Application Environment
  • Step 4: Create Components
  • Step 5: Create a Service and Interface
  • Step 6: Update App Routing and App Html
  • Step 7: Run the app
  • Screenshots:

Hi there, today we will show you how to develop login and registration in Angular 19 app using API. Most of the web apps today that have users or different types of users require login or authentication to safeguard the functionalities and data information of the system. And today, I will show you how to make a user login and registration in Angular 19.

Angular is a popular open-source front-end web application framework primarily maintained by Google and a community of developers. It’s used for building dynamic, single-page web applications (SPAs) and provides tools and architecture to streamline the development process. Angular enables developers to create rich, interactive, and responsive user interfaces.

In this tutorial, we will be using the REST API available that is made by Binaryboxtus. you can check it out here.

Video Tutorial:

Prerequisite:

  • ^18.19.1 or newer
  • @angular/cli

Step 1: Create An Angular 19 Project

First, select a folder that you want the Angular 19 project to be created then execute this command on Terminal or CMD :

ng new angular-login-register

Step 2: Install packages

After creating a fresh angular project, go to the vue.js project folder and install these packages:

  • bootstrap – a package of bootstrap framework
  • axios – a promised-based HTTP library that is used to request to a server or API.
npm i bootstrap
npm i axios

Modify the angular.json file to include Bootstrap’s CSS. Locate the styles array inside “build” and add:

angular.json

"styles": [
              "node_modules/bootstrap/dist/css/bootstrap.min.css",
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"
            ]

Step 3: Configure Application Environment

After installing the packages, we will now generate the app environment. We will declare the API base URL and API Key. Run the command below:

	ng generate environments

You can get the API_KEY here. Click the “Reveal Key” button and copy the API key.

image 26 Binarybox Tutorials

After running the command this will generate files for the environment. let’s add value to the environment.

environment.development.ts

export const environment = {
    production: false,
    apiUrl: 'https://mock-api.binaryboxtuts.com/',
    apiKey: 'insert_api_key_here'
};

After configuring the environment, let’s configure the axios on main.ts.

src\main.ts

import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';
 
import axios from 'axios';
import { environment } from './environments/environment.development';
 
axios.defaults.baseURL = environment.apiUrl
  
axios.interceptors.request.use(function (config) {
  config.headers['X-Binarybox-Api-Key'] = environment.apiKey
  return config;
});
 
 
bootstrapApplication(AppComponent, appConfig)
  .catch((err) => console.error(err));

Step 4: Create Components

We will now create the components. run the command below:

ng generate component login
ng generate component register
ng generate component dashboard

after running these commands it will create folders with files in them. We will now update the code on it.

src\app\login\login.component.ts

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { UserAuthService } from '../user-auth.service';


@Component({
  selector: 'app-login',
  imports: [CommonModule, RouterModule, FormsModule],
  templateUrl: './login.component.html',
  styleUrl: './login.component.css'
})
export class LoginComponent implements OnInit{
  email:string = ''
  password:string = ''
  isSubmitting:boolean = false
  validationErrors:Array<any> = []

  constructor(public userAuthService: UserAuthService, private router: Router) {}

  ngOnInit(): void {
    if(localStorage.getItem('token') != "" && localStorage.getItem('token') != null){
      this.router.navigateByUrl('/dashboard')
    }
  }

  loginAction() {
    this.isSubmitting = true;
    let payload = {
      email:this.email,
      password: this.password,
  }
    this.userAuthService.login(payload)
    .then(({data}) => {
      localStorage.setItem('token', data.token)
      this.router.navigateByUrl('/dashboard')
      return data
    }).catch(error => {
      this.isSubmitting = false;
      if (error.response.data.errors != undefined) {
        this.validationErrors = error.response.data.message
      }
      if (error.response.data.error != undefined) {
        this.validationErrors = error.response.data.error
      }
      return error
    })
  }
}

src\app\login\login.component.html

<div class="row justify-content-md-center mt-5">
    <div class="col-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title mb-4">Sign In</h5>
                <form >
                   
                    <p *ngIf="validationErrors.length > 0" class='text-center '>
                        <small class='text-danger'>{{validationErrors}}</small>
                    </p>
                    <div class="mb-3">
                        <label 
                            for="email"
                            class="form-label">
                                Email address
                        </label>
                        <input 
                            type="email"
                            class="form-control"
                            id="email"
                            name="email"
                            required
                            [(ngModel)]="email"
                        />
                    </div>
                    <div class="mb-3">
                        <label 
                            for="password"
                            class="form-label">Password
                        </label>
                        <input 
                            type="password"
                            class="form-control"
                            id="password"
                            name="password"
                            required
                            [(ngModel)]="password"
                        />
                    </div>
                    <div class="d-grid gap-2">
                        <button 
                            [disabled]="isSubmitting || (password.length <= 0 || email.length <= 0)"
                            (click)="loginAction()"
                            type="button"
                            class="btn btn-primary btn-block">Login</button>
                        <p class="text-center">Don't have account? <a routerLink="/register">Register here</a></p>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

src\app\register\register.component.ts

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { UserAuthService } from '../user-auth.service';

@Component({
  selector: 'app-register',
  imports: [CommonModule, RouterModule, FormsModule],
  templateUrl: './register.component.html',
  styleUrl: './register.component.css'
})
export class RegisterComponent implements OnInit{
  name:string = ''
  email:string = ''
  password:string = ''
  confirmPassword:string = ''
  isSubmitting:boolean = false
  validationErrors:any = []
 
  constructor(public userAuthService: UserAuthService, private router: Router) {}
 
  ngOnInit(): void {
    if(localStorage.getItem('token') != "" && localStorage.getItem('token') != null){
      this.router.navigateByUrl('/dashboard')
    }
  }
 
  registerAction() {
    this.isSubmitting = true;
    let payload = {
      name:this.name,
      email:this.email,
      password:this.password,
      confirmPassword:this.confirmPassword
    }
 
    this.userAuthService.register(payload)
    .then(({data}) => {
      localStorage.setItem('token', data.token)
      this.router.navigateByUrl('/dashboard')
      return data
    }).catch(error => {
      this.isSubmitting = false;
      if (error.response.data.errors != undefined) {
        this.validationErrors = error.response.data.errors
      }
      
      return error
    })
  }
}

src\app\register\register.component.html

<div class="row justify-content-md-center mt-5">
    <div class="col-4">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title mb-4">Register</h5>
                <form >
                    <div class="mb-3">
                        <label
                            for="name"
                            class="form-label">Name
                        </label>
                        <input
                            type="text"
                            class="form-control"
                            id="name"
                            name="name"
                            [(ngModel)]="name"
                        />
                        <div *ngIf="validationErrors?.name != undefined" class="flex flex-col">
                            <small  class="text-danger">
                                {{validationErrors.name[0]}}
                            </small >
                        </div>
                    </div>
                    <div class="mb-3">
                        <label
                            for="email"
                            class="form-label">
                                Email address
                        </label>
                        <input
                            type="email"
                            class="form-control"
                            id="email"
                            name="email"
                            required
                            [(ngModel)]="email"
                        />
                        <div *ngIf="validationErrors?.email != undefined" class="flex flex-col">
                            <small  class="text-danger">
                                {{validationErrors.email[0]}}
                            </small >
                        </div>
                    </div>
                    <div class="mb-3">
                        <label
                            for="password"
                            class="form-label">Password
                        </label>
                        <input
                            type="password"
                            class="form-control"
                            id="password"
                            name="password"
                            required
                            [(ngModel)]="password"
                        />
                        <div *ngIf="validationErrors?.password != undefined" class="flex flex-col">
                            <small  class="text-danger">
                                {{validationErrors.password[0]}}
                            </small >
                        </div>
                    </div>
                    <div class="mb-3">
                        <label
                            for="confirm-password"
                            class="form-label">Confirm Password
                        </label>
                        <input
                            type="password"
                            class="form-control"
                            id="confirm-password"
                            name="confirm-password"
                            required
                            [(ngModel)]="confirmPassword"
                        />
                    </div>
                    <div class="d-grid gap-2">
                        <button
                            [disabled]="isSubmitting || (password.length <= 0 || email.length <= 0 || confirmPassword.length <= 0)"
                            (click)="registerAction()"
                            type="button"
                            class="btn btn-primary btn-block">Register Now</button>
                        <p className="text-center">Have already an account <a routerLink="/login">Login here</a>
                        </p>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

src\app\dashboard\dashboard.component.ts

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { UserAuthService } from '../user-auth.service';
import { User } from '../user/user';

@Component({
  selector: 'app-dashboard',
  imports: [CommonModule, RouterModule, FormsModule],
  templateUrl: './dashboard.component.html',
  styleUrl: './dashboard.component.css'
})
export class DashboardComponent implements OnInit{
  user!:User
  constructor(public userAuthService: UserAuthService, private router: Router) {}
 
  ngOnInit(): void {
   if(localStorage.getItem('token') == "" || localStorage.getItem('token') == null){
      this.router.navigateByUrl('/')
    }else {
      this.userAuthService.getUser().then(({data})=>{
        this.user = data;
      })
    }
  }
 
  logoutAction () {
    this.userAuthService.logout().then(()=>{
      localStorage.setItem('token', "")
      this.router.navigateByUrl('/')
    }).catch(()=>{
      localStorage.setItem('token', "")
      this.router.navigateByUrl('/')
    })
   
  }
}

src\app\dashboard\dashboard.component.html

<div class="row justify-content-md-center">
    <div class="col-12">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <div class="container-fluid">
                <a class="navbar-brand" href="#">Dashboard</a>
                <div class="d-flex">
                    <ul class="navbar-nav">
                        <li class="nav-item">
                            <span (click)="logoutAction()" class="nav-link" aria-current="page" style="cursor: pointer;">Logout</span>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
        <h2 class="text-center mt-5">Welcome, {{user.name}}!</h2  >
    </div>
</div>

Step 5: Create a Service and Interface

We will now create the service and interface

ng generate service user-auth
ng generate interface user/user

Let’s update the code of service and interface.

src/app/user-auth.service.ts

import { Injectable } from '@angular/core';
import axios from 'axios';

@Injectable({
  providedIn: 'root'
})
export class UserAuthService {

  constructor() { }

  login(data:any): Promise<any>{
    let payload = {
      email: data.email,
      password: data.password
    }
 
    return axios.post('/api/login', payload)
  }

  register(data:any): Promise<any>{
    let payload = {
      name: data.name,
      email: data.email,
      password: data.password,
      password_confirmation: data.confirmPassword
    }
    
    return axios.post('/api/register', payload)
  }

  getUser(): Promise<any>{

    return axios.get('/api/user', { headers:{Authorization: 'Bearer ' + localStorage.getItem('token')}})
  }

  logout(): Promise<any>{

    return axios.post('/api/logout',{}, { headers:{Authorization: 'Bearer ' + localStorage.getItem('token')}})
  }
}

src/app/user/user.ts

export interface User {
    id: number;
    name: string;
    email: string;
}

Step 6: Update App Routing and App Html

After creating the component we will then update the app routing and app HTML.

src\app\app.routes.ts

import { Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { RegisterComponent } from './register/register.component';
import { DashboardComponent } from './dashboard/dashboard.component';

export const routes: Routes = [
    { path: '', redirectTo: 'login', pathMatch: 'full'},
    { path: 'login', component: LoginComponent },
    { path: 'register', component: RegisterComponent },
    { path: 'dashboard', component: DashboardComponent },
];

src\app\app.component.html

<div class="container">
  <router-outlet />
</div>

Step 7: Run the app

We’re all done, what is left is to run the app.

ng serve

Open this URL:

http://localhost:4200/

Screenshots:

Login Page

image 27 Binarybox Tutorials

Register Page

image 28 Binarybox Tutorials

Dashboard Page

image 29 Binarybox Tutorials

Angular 19LoginRegistration

Post navigation

Previous Post

How To Develop Angular 19 CRUD App using a REST API

Next Post

How To Add OTP Verification to Laravel 11 Authentication (Laravel Breeze)

Tags

AJAX Angular 17 Authentication Breeze CodeIgniter 3 CodeIgniter 4 CRUD DataTable Email Express Express Js Inertia JWT Laravel Laravel 7 Laravel 8; Laravel 9 Laravel 10 Laravel 11 Laravel 12 Laravel Breeze Laravel Passport Laravel Reverb Login Mailtrap MongoDB Multiple File Upload Node Js Page Numbering Payment Gateway PDF PHP QR Code React ReactJS Registration REST API Reverb Single Page Application SPA Stripe Symfony 5 Symfony 6 Symfony 7 Vue.js

Newsletter

Recent Posts

  • Laravel 12 – Integrating Stripe Payment Gateway Tutorial
  • Multiple File Upload in Laravel 12 Step-by-step Easy Tutorial
  • How To Merge PDF Files in Laravel 12
  • Laravel 12 QR Code Generator Tutorial
  • Generating PDF from HTML In Laravel 12

©2020 Binarytuts.com | Terms & Conditions | Privacy Policy

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
SAVE & ACCEPT

⭐ Do you find our blog helpful? ⭐

You can support us by buying us a coffee ☕
A coffee a day motivates us to make articles/tutorials 😎

🌐 buymeacoffee.com/binaryboxtm

Buy Me A Coffee

👉 Follow us on Linkedin to be updated 👈

Buy Me A Coffee