From 85e057f1ef11100af9e8ed6f7d4d244bcc4d6a92 Mon Sep 17 00:00:00 2001 From: Le Deng Date: Tue, 7 Mar 2017 14:10:20 -0500 Subject: [PATCH] latest --- bookstore-api/.idea/workspace.xml | 131 ++++++++++++------ .../com/bookstore/resource/UserResource.java | 40 ++++++ store-front/src/app/app.module.ts | 8 +- .../my-account/my-account.component.html | 55 +++++--- .../my-account/my-account.component.ts | 52 ++++++- .../my-profile/my-profile.component.css | 0 .../my-profile/my-profile.component.html | 3 + .../my-profile/my-profile.component.spec.ts | 28 ++++ .../my-profile/my-profile.component.ts | 94 +++++++++++++ .../components/nav-bar/nav-bar.component.html | 3 +- .../src/app/services/user.service.spec.ts | 16 +++ store-front/src/app/services/user.service.ts | 35 +++++ 12 files changed, 397 insertions(+), 68 deletions(-) create mode 100644 store-front/src/app/components/my-profile/my-profile.component.css create mode 100644 store-front/src/app/components/my-profile/my-profile.component.html create mode 100644 store-front/src/app/components/my-profile/my-profile.component.spec.ts create mode 100644 store-front/src/app/components/my-profile/my-profile.component.ts create mode 100644 store-front/src/app/services/user.service.spec.ts create mode 100644 store-front/src/app/services/user.service.ts diff --git a/bookstore-api/.idea/workspace.xml b/bookstore-api/.idea/workspace.xml index 3ec0a39..a7b52c4 100644 --- a/bookstore-api/.idea/workspace.xml +++ b/bookstore-api/.idea/workspace.xml @@ -16,11 +16,31 @@ - + + + + + + + + + + + + + + + + + + + + + - - + + @@ -29,7 +49,7 @@ - + @@ -99,11 +119,11 @@ @@ -121,10 +141,9 @@ - @@ -272,6 +291,36 @@ - - + @@ -869,11 +918,11 @@ - + - + @@ -1185,24 +1234,6 @@ - - - - - - - - - - - - - - - - - - @@ -1239,14 +1270,6 @@ - - - - - - - - @@ -1255,10 +1278,34 @@ + + + + + + + + + + + + + + + + - - + + + + + + + + + + diff --git a/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java b/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java index 4ae776c..6316492 100644 --- a/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java +++ b/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java @@ -133,4 +133,44 @@ public class UserResource { return new ResponseEntity("User Added Successfully!", HttpStatus.OK); } + + @RequestMapping("/forgetPassword") + public ResponseEntity forgetPassword(@RequestBody String email, + HttpServletRequest request, + Model model) { + model.addAttribute("classActiveForgetPassword", "true"); + + User user = userService.findByEmail(email); + + if (user == null) { + model.addAttribute("emailNotExists", true); + return new ResponseEntity("Email not found!", HttpStatus.BAD_REQUEST); + + } + + String password = SecurityUtility.randomPassword(); + + String encryptedPassword = SecurityUtility.passwordEncoder().encode(password); + user.setPassword(encryptedPassword); + + userService.save(user); + + String token = UUID.randomUUID().toString(); + userService.createPasswordResetTokenForUser(user, token); + + String appUrl = + "http://" + request.getServerName() + + ":" + request.getServerPort() + + request.getContextPath(); + + SimpleMailMessage newEmail = + mailConstructor.constructResetTokenEmail(appUrl, request.getLocale(), token, user, password); + + mailSender.send(newEmail); + + model.addAttribute("forgetPasswordEmailSent", true); + + return new ResponseEntity("Email sent!", HttpStatus.OK); + + } } diff --git a/store-front/src/app/app.module.ts b/store-front/src/app/app.module.ts index 0b39f72..1714959 100644 --- a/store-front/src/app/app.module.ts +++ b/store-front/src/app/app.module.ts @@ -12,14 +12,17 @@ import { HomeComponent } from './components/home/home.component'; import { NavBarComponent } from './components/nav-bar/nav-bar.component'; import { LoginService } from './services/login.service'; +import { UserService } from './services/user.service'; import { MyAccountComponent } from './components/my-account/my-account.component'; +import { MyProfileComponent } from './components/my-profile/my-profile.component'; @NgModule({ declarations: [ AppComponent, HomeComponent, NavBarComponent, - MyAccountComponent + MyAccountComponent, + MyProfileComponent ], imports: [ BrowserModule, @@ -29,7 +32,8 @@ import { MyAccountComponent } from './components/my-account/my-account.component MaterialModule ], providers: [ - LoginService + LoginService, + UserService ], bootstrap: [AppComponent] }) diff --git a/store-front/src/app/components/my-account/my-account.component.html b/store-front/src/app/components/my-account/my-account.component.html index d06f278..44c84fb 100644 --- a/store-front/src/app/components/my-account/my-account.component.html +++ b/store-front/src/app/components/my-account/my-account.component.html @@ -16,29 +16,10 @@
- -
-
An email has been sent to email address you just registered. Please validate your email address and update your password info.
-
-
-  Username already exists. Choose a different one. - -

Enter your username, special charaters are not allowed. -

-
-
-  Email already exists. Choose a different one. - -

A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.

-
- -
-
-
Incorrect username or password.
-
+
@@ -54,7 +35,39 @@
- Content 2 + +
+
An email has been sent to email address you just registered. Please validate your email address and update your password info.
+
+
+  Username already exists. Choose a different one. + +

Enter your username, special charaters are not allowed. +

+
+
+  Email already exists. Choose a different one. + +

A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.

+
+ +
+
+
+ +
+
Email doesn't exists.
+
Email sent.
+
+
+ + +

Enter your registered email address.

+
+ +
+
+
diff --git a/store-front/src/app/components/my-account/my-account.component.ts b/store-front/src/app/components/my-account/my-account.component.ts index 7a75aac..25ca601 100644 --- a/store-front/src/app/components/my-account/my-account.component.ts +++ b/store-front/src/app/components/my-account/my-account.component.ts @@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core'; import {AppConst} from '../../constants/app-const'; import {Router} from "@angular/router"; import {LoginService} from "../../services/login.service"; +import {UserService} from "../../services/user.service"; @Component({ selector: 'app-my-account', @@ -15,10 +16,20 @@ export class MyAccountComponent implements OnInit { private loggedIn = false; private credential = {'username':'', 'password':''}; - constructor (private loginService: LoginService, private router: Router){ + private emailSent:boolean = false; + private usernameExists:boolean = false; + private emailExists:boolean = false; + private username:string; + private email:string; + + private emailNotExists: boolean = false; + private forgetPasswordEmailSent: boolean = false; + private recoverEmail:string; + + constructor (private loginService: LoginService, private userService: UserService, private router: Router){ } - onSubmit() { + onLogin() { this.loginService.sendCredential(this.credential.username, this.credential.password).subscribe( res=>{ console.log(res); @@ -33,6 +44,43 @@ export class MyAccountComponent implements OnInit { ); } + onNewAccount() { + this.usernameExists=false; + this.emailExists=false; + this.emailSent = false; + + this.userService.newUser(this.username, this.email).subscribe( + res => { + console.log(res); + this.emailSent = true; + }, + error => { + console.log(error.text()); + let errorMessage=error.text(); + if (errorMessage==="usernameExists") this.usernameExists=true; + if (errorMessage==="emailExists") this.emailExists=true; + } + ); + } + + onForgetPassword() { + this.forgetPasswordEmailSent = false; + this.emailNotExists = false; + + this.userService.retrievePassword(this.recoverEmail).subscribe( + res => { + console.log(res); + this.emailSent = true; + }, + error => { + console.log(error.text()); + let errorMessage=error.text(); + if (errorMessage==="usernameExists") this.usernameExists=true; + if (errorMessage==="emailExists") this.emailExists=true; + } + ); + } + ngOnInit() { this.loginService.checkSession().subscribe( res => { diff --git a/store-front/src/app/components/my-profile/my-profile.component.css b/store-front/src/app/components/my-profile/my-profile.component.css new file mode 100644 index 0000000..e69de29 diff --git a/store-front/src/app/components/my-profile/my-profile.component.html b/store-front/src/app/components/my-profile/my-profile.component.html new file mode 100644 index 0000000..7782815 --- /dev/null +++ b/store-front/src/app/components/my-profile/my-profile.component.html @@ -0,0 +1,3 @@ +

+ my-profile works! +

diff --git a/store-front/src/app/components/my-profile/my-profile.component.spec.ts b/store-front/src/app/components/my-profile/my-profile.component.spec.ts new file mode 100644 index 0000000..6c60227 --- /dev/null +++ b/store-front/src/app/components/my-profile/my-profile.component.spec.ts @@ -0,0 +1,28 @@ +/* tslint:disable:no-unused-variable */ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { By } from '@angular/platform-browser'; +import { DebugElement } from '@angular/core'; + +import { MyProfileComponent } from './my-profile.component'; + +describe('MyProfileComponent', () => { + let component: MyProfileComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ MyProfileComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(MyProfileComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/store-front/src/app/components/my-profile/my-profile.component.ts b/store-front/src/app/components/my-profile/my-profile.component.ts new file mode 100644 index 0000000..b7e8b06 --- /dev/null +++ b/store-front/src/app/components/my-profile/my-profile.component.ts @@ -0,0 +1,94 @@ +import { Component, OnInit } from '@angular/core'; +import {AppConst} from '../../constants/app-const'; +import {Router} from "@angular/router"; +import {LoginService} from "../../services/login.service"; +import {UserService} from "../../services/user.service"; + +@Component({ + selector: 'app-my-profile', + templateUrl: './my-profile.component.html', + styleUrls: ['./my-profile.component.css'] +}) +export class MyProfileComponent implements OnInit { + + private serverPath = AppConst.serverPath; + private loginError:boolean = false; + private loggedIn = false; + private credential = {'username':'', 'password':''}; + + private emailSent:boolean = false; + private usernameExists:boolean = false; + private emailExists:boolean = false; + private username:string; + private email:string; + + private emailNotExists: boolean = false; + private forgetPasswordEmailSent: boolean = false; + private recoverEmail:string; + + constructor (private loginService: LoginService, private userService: UserService, private router: Router){ + } + + onLogin() { + this.loginService.sendCredential(this.credential.username, this.credential.password).subscribe( + res=>{ + console.log(res); + localStorage.setItem("xAuthToken", res.json().token); + this.loggedIn=true; + location.reload(); + this.router.navigate(['/home']); + }, + error=>{ + this.loggedIn=false; + } + ); + } + + onNewAccount() { + this.usernameExists=false; + this.emailExists=false; + this.emailSent = false; + + this.userService.newUser(this.username, this.email).subscribe( + res => { + console.log(res); + this.emailSent = true; + }, + error => { + console.log(error.text()); + let errorMessage=error.text(); + if (errorMessage==="usernameExists") this.usernameExists=true; + if (errorMessage==="emailExists") this.emailExists=true; + } + ); + } + + onForgetPassword() { + this.forgetPasswordEmailSent = false; + this.emailNotExists = false; + + this.userService.retrievePassword(this.recoverEmail).subscribe( + res => { + console.log(res); + this.emailSent = true; + }, + error => { + console.log(error.text()); + let errorMessage=error.text(); + if (errorMessage==="usernameExists") this.usernameExists=true; + if (errorMessage==="emailExists") this.emailExists=true; + } + ); + } + + ngOnInit() { + this.loginService.checkSession().subscribe( + res => { + this.loggedIn=true; + }, + error => { + this.loggedIn=false; + } + ); + +} diff --git a/store-front/src/app/components/nav-bar/nav-bar.component.html b/store-front/src/app/components/nav-bar/nav-bar.component.html index 55d86eb..1020847 100644 --- a/store-front/src/app/components/nav-bar/nav-bar.component.html +++ b/store-front/src/app/components/nav-bar/nav-bar.component.html @@ -20,7 +20,8 @@
  • -
  • MY ACCOUNT
  • +
  • MY ACCOUNT
  • +
  • LOGOUT
  • diff --git a/store-front/src/app/services/user.service.spec.ts b/store-front/src/app/services/user.service.spec.ts new file mode 100644 index 0000000..857b499 --- /dev/null +++ b/store-front/src/app/services/user.service.spec.ts @@ -0,0 +1,16 @@ +/* tslint:disable:no-unused-variable */ + +import { TestBed, async, inject } from '@angular/core/testing'; +import { UserService } from './user.service'; + +describe('UserService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [UserService] + }); + }); + + it('should ...', inject([UserService], (service: UserService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/store-front/src/app/services/user.service.ts b/store-front/src/app/services/user.service.ts new file mode 100644 index 0000000..e8b89a5 --- /dev/null +++ b/store-front/src/app/services/user.service.ts @@ -0,0 +1,35 @@ +import { Injectable } from '@angular/core'; +import {AppConst} from '../constants/app-const'; +import {Http, Headers} from '@angular/http'; + +@Injectable() +export class UserService { + private serverPath:string = AppConst.serverPath; + + constructor(private http:Http) { } + + newUser(username:string, email:string) { + let url = this.serverPath+"/user/newUser"; + let userInfo = { + "username" : username, + "email" : email + }; + let tokenHeader = new Headers ({ + 'Content-Type': 'application/json', + 'x-auth-token' : localStorage.getItem("xAuthToken") + }); + return this.http.post(url, JSON.stringify(userInfo), {headers : tokenHeader}); + } + + retrievePassword(email:string) { + let url = this.serverPath+"/user/forgetPassword"; + let userInfo = { + "email" : email + }; + let tokenHeader = new Headers ({ + 'Content-Type': 'application/json', + 'x-auth-token' : localStorage.getItem("xAuthToken") + }); + return this.http.post(url, JSON.stringify(userInfo), {headers : tokenHeader}); + } +}