mirror of
				https://github.com/avatao-content/frontend-tutorial-framework
				synced 2025-11-04 02:52:55 +00:00 
			
		
		
		
	Add logic to Login
This commit is contained in:
		@@ -1,3 +1,5 @@
 | 
				
			|||||||
 | 
					import { HttpClientModule } from '@angular/common/http';
 | 
				
			||||||
 | 
					import { FormsModule } from '@angular/forms';
 | 
				
			||||||
import { BrowserModule } from '@angular/platform-browser';
 | 
					import { BrowserModule } from '@angular/platform-browser';
 | 
				
			||||||
import { NgModule } from '@angular/core';
 | 
					import { NgModule } from '@angular/core';
 | 
				
			||||||
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
 | 
					import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
 | 
				
			||||||
@@ -25,6 +27,8 @@ import { WebSocketService } from './websocket.service';
 | 
				
			|||||||
  ],
 | 
					  ],
 | 
				
			||||||
  imports: [
 | 
					  imports: [
 | 
				
			||||||
    BrowserModule,
 | 
					    BrowserModule,
 | 
				
			||||||
 | 
					    FormsModule,
 | 
				
			||||||
 | 
					    HttpClientModule,
 | 
				
			||||||
    NgbModule.forRoot(),
 | 
					    NgbModule.forRoot(),
 | 
				
			||||||
    AceEditorModule,
 | 
					    AceEditorModule,
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
@@ -32,6 +36,8 @@ import { WebSocketService } from './websocket.service';
 | 
				
			|||||||
    MarkdownService,
 | 
					    MarkdownService,
 | 
				
			||||||
    WebSocketService
 | 
					    WebSocketService
 | 
				
			||||||
  ],
 | 
					  ],
 | 
				
			||||||
  bootstrap: [AppComponent]
 | 
					  bootstrap: [
 | 
				
			||||||
 | 
					    AppComponent
 | 
				
			||||||
 | 
					  ]
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
export class AppModule { }
 | 
					export class AppModule { }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
<div>
 | 
					<div>
 | 
				
			||||||
  <form >
 | 
					  <h1>Login page</h1>
 | 
				
			||||||
 | 
					  <form #loginForm="ngForm" (submit)="onSubmit()" [hidden]="submitted">
 | 
				
			||||||
    <div class="form-group">
 | 
					    <div class="form-group">
 | 
				
			||||||
      <label for="email_input">Email:</label>
 | 
					      <label for="email_input">Email:</label>
 | 
				
			||||||
      <input
 | 
					      <input
 | 
				
			||||||
@@ -8,6 +9,7 @@
 | 
				
			|||||||
        type="email"
 | 
					        type="email"
 | 
				
			||||||
        name="email"
 | 
					        name="email"
 | 
				
			||||||
        placeholder="Enter email"
 | 
					        placeholder="Enter email"
 | 
				
			||||||
 | 
					        [(ngModel)]="model.email"
 | 
				
			||||||
      >
 | 
					      >
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <div class="form-group">
 | 
					    <div class="form-group">
 | 
				
			||||||
@@ -18,9 +20,17 @@
 | 
				
			|||||||
        type="password"
 | 
					        type="password"
 | 
				
			||||||
        name="password"
 | 
					        name="password"
 | 
				
			||||||
        placeholder="Password"
 | 
					        placeholder="Password"
 | 
				
			||||||
 | 
					        [(ngModel)]="model.password"
 | 
				
			||||||
      >
 | 
					      >
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    <button type="submit" class="btn btn-primary">Submit</button>
 | 
					    <button type="submit" class="btn btn-primary">Submit</button>
 | 
				
			||||||
  </form>
 | 
					  </form>
 | 
				
			||||||
 | 
					  <div [hidden]="!submitted">
 | 
				
			||||||
 | 
					    <p>
 | 
				
			||||||
 | 
					      Logged in as {{logged_in_email}}.
 | 
				
			||||||
 | 
					      You <em><span *ngIf="!is_admin">don't </span>have</em> admin privileges.
 | 
				
			||||||
 | 
					    </p>
 | 
				
			||||||
 | 
					  </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,7 @@
 | 
				
			|||||||
 | 
					import { HttpClient } from '@angular/common/http';
 | 
				
			||||||
import { Component, OnInit } from '@angular/core';
 | 
					import { Component, OnInit } from '@angular/core';
 | 
				
			||||||
 | 
					import { Observable } from 'rxjs/Observable';
 | 
				
			||||||
 | 
					import { Login } from './login';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Component({
 | 
					@Component({
 | 
				
			||||||
  selector: 'app-login',
 | 
					  selector: 'app-login',
 | 
				
			||||||
@@ -6,9 +9,26 @@ import { Component, OnInit } from '@angular/core';
 | 
				
			|||||||
  styleUrls: ['./login.component.scss']
 | 
					  styleUrls: ['./login.component.scss']
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
export class LoginComponent implements OnInit {
 | 
					export class LoginComponent implements OnInit {
 | 
				
			||||||
  constructor() { }
 | 
					  model = new Login('', '');
 | 
				
			||||||
 | 
					  submitted = false;
 | 
				
			||||||
  ngOnInit() {
 | 
					  is_admin: boolean;
 | 
				
			||||||
 | 
					  logged_in_email: string;
 | 
				
			||||||
 | 
					  constructor(
 | 
				
			||||||
 | 
					    private http: HttpClient
 | 
				
			||||||
 | 
					  ) {}
 | 
				
			||||||
 | 
					  onSubmit() {
 | 
				
			||||||
 | 
					    this.postLogin(this.model).subscribe(
 | 
				
			||||||
 | 
					      res => {
 | 
				
			||||||
 | 
					        this.logged_in_email = res['email'];
 | 
				
			||||||
 | 
					        this.is_admin = res['is_admin'];
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    this.submitted = true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  postLogin(login: Login): Observable<any> {
 | 
				
			||||||
 | 
					    return this.http.post<Login>('/login', login);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ngOnInit() {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								src/app/login/login.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/app/login/login.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
				
			|||||||
 | 
					export class Login {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  constructor(
 | 
				
			||||||
 | 
					    public email: string,
 | 
				
			||||||
 | 
					    public password: string
 | 
				
			||||||
 | 
					  ) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user