This Angular code defines an AuthModule that handles user login, including a login form component, routing, and authentication logic using an AuthService.
npm run import -- "Display login form"import {RouterModule, Routes} from '@angular/router';
import {Component, ModuleWithProviders, NgModule} from '@angular/core';
import {COMMON_MODULES} from '../../../imports/core.module';
import {AuthService} from '../../../imports/auth.service';
@Component({
selector: 'bc-login',
template: `
<form>
<md-input-container>
<input mdInput name="signupEmail" required type="email"
placeholder="Username"
maxlength="100" [(ngModel)]="username">
</md-input-container>
<br/>
<md-input-container>
<input mdInput name="signupPassword" required type="password" maxlength="25"
placeholder="Password"
length="40" [(ngModel)]="password">
</md-input-container>
<button md-raised-button color="primary"
id="signupSubmit" type="submit" (click)="onLogin()">
Login
</button>
</form>
`,
styleUrls: ['./login.component.scss']
})
export class LoginComponent {
username: string;
password: string;
service: AuthService
constructor(public
) {
}
onLogin()
:
void {
this.service.login(this.username, this.password).subscribe(r => {
console.log(r);
});
}
}
export const COMPONENTS = [
LoginComponent
];
export const authRoutes: Routes = [
{
path: '',
component: LoginComponent,
data: {roles: ['anonymous', 'user']}
}
];
export const routing: ModuleWithProviders = RouterModule.forChild(authRoutes);
@NgModule({
imports: [
...COMMON_MODULES,
routing
],
declarations: COMPONENTS,
exports: COMPONENTS
})
export class AuthModule {
}
// auth.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { LoginComponent } from './login/login.component';
import { AuthService } from './auth.service';
import { ReactiveFormsModule } from '@angular/forms';
const authRoutes: Routes = [
{
path: '',
component: LoginComponent,
data: { roles: ['anonymous', 'user'] }
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(authRoutes),
ReactiveFormsModule
],
declarations: [LoginComponent],
exports: [LoginComponent]
})
export class AuthModule { }
export const COMPONENTS = [LoginComponent];
This Angular code defines a module (AuthModule) responsible for handling user login functionality.
Here's a breakdown:
1. Imports:
core.module and auth.service).2. LoginComponent:
LoginComponent) responsible for displaying the login form.
md-input-container, md-raised-button) to create a form with fields for username and password.username and password properties store the user's input.onLogin() method is triggered when the login button is clicked.AuthService.login() method, passing the username and password.3. Routing:
authRoutes) that maps the empty path (/) to the LoginComponent.data property specifies that this route is accessible to anonymous users and users with the "user" role.4. AuthModule:
AuthModule which:
COMMON_MODULES, the routing configuration, and the LoginComponent.LoginComponent as part of this module.LoginComponent so it can be used in other modules.In summary: This code sets up a basic Angular login system with a form, authentication logic, and routing.