Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 6305

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T05:19:07+00:00 2024-11-27T05:19:07+00:00

Angular 18.1 – Create a simple login using Angular Material

  • 60k

We have some new project in our team… we need to learn about code into angular to create and i become some difficulties to get a rapid learning and creating.

As a team we trying to find some rapid tutorials about some functionality, the login was one and is so difficult to find we create this login using multiple web pages to bring the functionality we need in our project.

Create base project

First thing we need to do is to install NodeJS if not have on our computer, got the latest evrsion from official site: NodeJS Download.

Then install the latest version of angular.

npm install -g @angular/cli 
Enter fullscreen mode Exit fullscreen mode

Then following the documentation in angular official site, we need to create a project.

ng new <project-name> 
Enter fullscreen mode Exit fullscreen mode

You will be presented with some configuration options for your project. Use the arrow and enter keys to navigate and select which options you desire.

If you don't have any preferences, just hit the enter key to take the default options and continue with the setup.

After you select the configuration options and the CLI runs through the setup, you should see the following message:

✔ Packages installed successfully.  Successfully initialized git. 
Enter fullscreen mode Exit fullscreen mode

At this point, you're now ready to run your project locally!, to do this we need to move into the project folder we created and then run the follow line on the terminal:

ng serve 
Enter fullscreen mode Exit fullscreen mode

After successfully loads the proyect you can go into http://localhost:4200/ to see your application running, now you see the follow view.

Image description

Install Angular Material

To install angular Material 18.1, we need to run the follow line on the terminal.

ng add @angular/material 
Enter fullscreen mode Exit fullscreen mode

Then the terminal ask you to: select the theme, select if you want to install globally the material font, and select if you want to install the Angular Material animations, the follow are the files this installation modify.

  • Add project dependencies to package.json
  • Add the Roboto font to your index.html
  • Add the Material Design icon font to your index.html
  • Add a few global CSS styles to:
    • Remove margins from body
    • Set height: 100% on html and body
    • Set Roboto as the default application font

You're done! Angular Material is now configured to be used in your application.

Create login component

Now we need to create a new folder in the project inside folder /app called authentication

Image description

Then we need to create the new component, writing the follow line on terminal:

ng g c /authentication/login 
Enter fullscreen mode Exit fullscreen mode

And you see the follow output on the terminal:

CREATE src/app/authentication/login/login.component.html (21 bytes) CREATE src/app/authentication/login/login.component.spec.ts (608 bytes) CREATE src/app/authentication/login/login.component.ts (243 bytes) CREATE src/app/authentication/login/login.component.scss (0 bytes) 
Enter fullscreen mode Exit fullscreen mode

The follow step in the component was to set the styles, i suggest the follow:

login.component.css

mat-card {     max-width: 400px;     margin: 2em auto;     text-align: center; } 

mat-form-field {
display: block;
}

.card-title {
color: #646464;
}

:host {
display: flex;
flex-direction: column;
align-items: flex-start;
}

footer {
width: 100%;
font-size: .9em;
color: darkgray;
text-align: center;
}

Also we add the follow html code to: > login.component.html ```html <mat-card>     <mat-card-header>         <h2>Sistema Soluciones Telcel</h2>     </mat-card-header>     <mat-card-content>         <form #loginForm="ngForm" (submit)="login()">             <h4 class="card-title">Acceso</h4>             <mat-error *ngIf="!loginValid">                 El usuario y contraseña no son correctos!.             </mat-error>             <mat-form-field>                 <mat-label>Usuario</mat-label>                 <input matInput placeholder="Usuario" [(ngModel)]="user" name="username" required>             </mat-form-field>             <mat-form-field>                 <mat-label>Contraseña</mat-label>                 <input matInput type="password" placeholder="Contraseña" [(ngModel)]="password" name="password"                     required>             </mat-form-field>             <button mat-raised-button color="primary" [disabled]="!loginForm.form.valid">Login</button>         </form>     </mat-card-content> </mat-card>  <footer>     Copyright © {{year}}     <a href="#" rel="" target="_blank" title="Retrogemu">Retrogemu</a> </footer> 
Enter fullscreen mode Exit fullscreen mode

And need to write the follow code on the latest file, this code have all the imports needed to get the angular material on the view, and handle the login functionality calling an API we created previously in our team or handle locally in our angular porject.

login.component.ts

import { Component, ChangeDetectionStrategy } from '@angular/core'; import { RouterOutlet, Router } from '@angular/router'; import { AuthService } from '../../core/services/auth.service'; import { FormsModule } from '@angular/forms'; import { CommonModule } from '@angular/common'; import { MatCardModule } from '@angular/material/card'; import { MatInputModule } from '@angular/material/input'; import { MatButtonModule } from '@angular/material/button'; import { MatFormFieldModule } from '@angular/material/form-field'; 

const materialModules = [
RouterOutlet,
FormsModule,
CommonModule,
MatCardModule,
MatInputModule,
MatButtonModule,
MatFormFieldModule
];

@Component({
selector: 'app-login',
standalone: true,
imports: [materialModules],
templateUrl: './login.component.html',
styleUrl: './login.component.css',
changeDetection: ChangeDetectionStrategy.OnPush
})

export default class LoginComponent {
user: string = '';
password: string = '';
loginValid: boolean = true;
year: number = new Date().getFullYear();

constructor(private authService: AuthService, private router: Router) {

}

login(): void {
this.authService.login(btoa(this.user), btoa(this.password)).subscribe({
next: () => {
this.loginValid = true;
this.router.navigate(['index']);
},
error: (err) => this.loginValid = false
});
}
}

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sp1gi3gki10kl8zwzo5i.png)  Now we have a functional login for our project in Angular 18.1 and Angular Material 18.1, also have a user and password handler to send into API or local movement.  
Enter fullscreen mode Exit fullscreen mode

angularjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.