added svcapi ui and camunda code
[it/otf.git] / otf-frontend / client / src / app / login / login.component.ts
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 import { Component, OnInit } from '@angular/core';\r
18 import { Router, ActivatedRoute } from '@angular/router';\r
19 import { routerTransition } from '../router.animations';\r
20 import { HttpClient } from '@angular/common/http';\r
21 import { AppGlobals } from '../app.global';\r
22 import { UserService } from '../shared/services/user.service';\r
23 import { CookieService } from 'ngx-cookie-service';\r
24 import { AuthService } from 'app/shared/services/auth.service';\r
25 import { AlertModalComponent } from '../shared/modules/alert-modal/alert-modal.component';\r
26 import { MatDialog } from '@angular/material';\r
27 \r
28 \r
29 \r
30 @Component({\r
31     selector: 'app-login',\r
32     templateUrl: './login.component.html',\r
33     providers: [],\r
34     styleUrls: ['./login.component.scss'],\r
35     animations: [routerTransition()]\r
36 })\r
37 export class LoginComponent implements OnInit {\r
38     public User;\r
39     public authResult;\r
40     public returnUrl;\r
41     public loginFailed = false;\r
42 \r
43     constructor(public router: Router, \r
44         private route: ActivatedRoute,\r
45         private http: HttpClient,\r
46         private  _global: AppGlobals,\r
47         private cookie: CookieService,\r
48         private dialog: MatDialog,\r
49         private auth: AuthService\r
50     ) {}\r
51 \r
52     ngOnInit() {\r
53         this.User={};\r
54         this.User.email = "";\r
55         this.User.password = "";\r
56         this.authResult={};\r
57 \r
58         this.auth.logout();\r
59 \r
60         this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';\r
61     }\r
62 \r
63     onLoggedin() {\r
64         //alert("User email: " + this.User.email + " User password: " + this.User.password);\r
65         this.auth.login(this.User) //need to use /authorization\r
66             .subscribe(\r
67                 (authResult) => {\r
68                     if(this.cookie.check('access_token')){\r
69                         this.router.navigate([this.returnUrl]);\r
70                     }else {\r
71                         if (authResult['user'] && !authResult['user']['enabled']) {\r
72                             this.dialog.open(AlertModalComponent, {\r
73                                 width: '450px',\r
74                                 data: {\r
75                                     type: 'ok',\r
76                                     message: "Your account is not yet enabled. Please wait for approval."\r
77                                 }\r
78                             });\r
79                         }\r
80                         else {\r
81                             this.dialog.open(AlertModalComponent, {\r
82                                 width: '450px',\r
83                                 data: {\r
84                                     type: 'alert',\r
85                                     message: "Something went wrong... Please Refresh and try again."\r
86                                 }\r
87                             });\r
88                         }\r
89                     }\r
90                 },\r
91                 (error) => {\r
92                     this.loginFailed = true;\r
93                     this.dialog.open(AlertModalComponent, {\r
94                         width: '450px',\r
95                         data: {\r
96                             type: 'alert',\r
97                             message: error + " Please try again"\r
98                         }\r
99                     });\r
100                 });\r
101     }\r
102 }\r