diff --git a/bookstore-api/.idea/workspace.xml b/bookstore-api/.idea/workspace.xml index ea61eed..bf9b572 100644 --- a/bookstore-api/.idea/workspace.xml +++ b/bookstore-api/.idea/workspace.xml @@ -16,6 +16,16 @@ + + + + + + + + + + @@ -40,11 +50,11 @@ - + - + @@ -110,6 +120,11 @@ + + + password + + @@ -153,13 +168,13 @@ - + @@ -177,10 +192,10 @@ - - - - + + + + @@ -957,15 +972,15 @@ - + - + - + @@ -985,11 +1000,11 @@ - + - + @@ -1009,7 +1024,7 @@ - + @@ -1270,14 +1285,6 @@ - - - - - - - - @@ -1365,7 +1372,15 @@ - + + + + + + + + + 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 9c4878e..f6ca3f3 100644 --- a/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java +++ b/bookstore-api/src/main/java/com/bookstore/resource/UserResource.java @@ -1,5 +1,6 @@ package com.bookstore.resource; +import com.bookstore.config.SecurityConfig; import com.bookstore.config.SecurityUtility; import com.bookstore.domain.User; import com.bookstore.domain.security.PasswordResetToken; @@ -17,6 +18,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @@ -46,23 +48,18 @@ public class UserResource { @RequestMapping(value = "/newUser", method = RequestMethod.POST) public ResponseEntity newUser(HttpServletRequest request, - @RequestBody HashMap mapper, - Model model + @RequestBody HashMap mapper ) throws Exception { String username = mapper.get("username"); String userEmail = mapper.get("email"); // check username exists if (userService.findByUsername(username) != null) { - model.addAttribute("usernameExists", true); - return new ResponseEntity("usernameExists", HttpStatus.BAD_REQUEST); } // check email address exists if (userService.findByEmail(userEmail) != null) { - model.addAttribute("emailExists", true); - return new ResponseEntity("emailExists", HttpStatus.BAD_REQUEST); } @@ -95,27 +92,23 @@ public class UserResource { mailSender.send(email); - model.addAttribute("emailSent", "true"); return new ResponseEntity("User Added Successfully!", HttpStatus.OK); } @RequestMapping("/addNewUser") public ResponseEntity addNewUser( - Locale locale, Model model, + Locale locale, @RequestParam("token") String token) { PasswordResetToken passToken = userService.getPasswordResetToken(token); if (passToken == null) { - String message = "Invalid Token."; - model.addAttribute("message", message); return new ResponseEntity("Can't Add User!", HttpStatus.BAD_REQUEST); } Calendar cal = Calendar.getInstance(); if ((passToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) { - model.addAttribute("message", "Token has expired."); return new ResponseEntity("Can't Add User!", HttpStatus.BAD_REQUEST); } @@ -130,21 +123,18 @@ public class UserResource { SecurityContextHolder.getContext().setAuthentication(authentication); - model.addAttribute("user", user); return new ResponseEntity("User Added Successfully!", HttpStatus.OK); } @RequestMapping("/forgetPassword") public ResponseEntity forgetPassword(@RequestBody String email, - HttpServletRequest request, - Model model) { - model.addAttribute("classActiveForgetPassword", "true"); + HttpServletRequest request + ) { User user = userService.findByEmail(email); if (user == null) { - model.addAttribute("emailNotExists", true); return new ResponseEntity("Email not found!", HttpStatus.BAD_REQUEST); } @@ -169,14 +159,74 @@ public class UserResource { mailSender.send(newEmail); - model.addAttribute("forgetPasswordEmailSent", true); - return new ResponseEntity("Email sent!", HttpStatus.OK); } + @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST) + public ResponseEntity profileInfo( + @RequestBody HashMap mapper + ) throws Exception { + + String email = (String) mapper.get("email"); + String username = (String) mapper.get("username"); + String firstName = (String) mapper.get("firstName"); + String lastName = (String) mapper.get("lastName"); + int id = (Integer) mapper.get("id"); + String newPassword = (String) mapper.get("newPassword"); + String currentPassword = (String) mapper.get("currentPassword"); + + User currentUser = userService.findById(Long.valueOf(id)); +// + if (currentUser == null) { + throw new Exception("User not found"); + } + +// check email address exists + if (userService.findByEmail(email) != null) { + if (userService.findByEmail(email).getId() != currentUser.getId()) { + return new ResponseEntity("Email not found!", HttpStatus.BAD_REQUEST); + + } + } + +// check username exists + if (userService.findByUsername(username) != null) { + if (userService.findByUsername(username).getId() != currentUser.getId()) { + return new ResponseEntity("Username not found!", HttpStatus.BAD_REQUEST); + } + } + + SecurityConfig securityConfig = new SecurityConfig(); + +// update password + if (newPassword != null && !newPassword.isEmpty() && !newPassword.equals("")) { + BCryptPasswordEncoder passwordEncoder = SecurityUtility.passwordEncoder(); + String dbPassword = currentUser.getPassword(); + System.out.println(currentPassword); + System.out.println(dbPassword); + System.out.println(passwordEncoder.matches(currentPassword, dbPassword)); + if (currentPassword.equals(dbPassword)) { + currentUser.setPassword(passwordEncoder.encode(newPassword)); + } else { + return new ResponseEntity("Incorrect current password!", HttpStatus.OK); + + } + } + + currentUser.setFirstName(firstName); + currentUser.setLastName(lastName); + currentUser.setUsername(username); + currentUser.setEmail(email); + + userService.save(currentUser); + + return new ResponseEntity("Update Success!", HttpStatus.OK); + + } + @RequestMapping("/getCurrentUser") - public User getCurrentUser(Principal principal){ + public User getCurrentUser(Principal principal) { User user = userService.findByUsername(principal.getName()); return user; diff --git a/bookstore-api/target/classes/com/bookstore/resource/UserResource.class b/bookstore-api/target/classes/com/bookstore/resource/UserResource.class index 327acc1..ca70f95 100644 Binary files a/bookstore-api/target/classes/com/bookstore/resource/UserResource.class and b/bookstore-api/target/classes/com/bookstore/resource/UserResource.class differ 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 index a270abc..753ec5c 100644 --- a/store-front/src/app/components/my-profile/my-profile.component.html +++ b/store-front/src/app/components/my-profile/my-profile.component.html @@ -24,7 +24,7 @@ Update Success! - + User info updated. @@ -55,7 +55,7 @@ Password - + Confirm Password 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 index cc15112..c11208c 100644 --- a/store-front/src/app/components/my-profile/my-profile.component.ts +++ b/store-front/src/app/components/my-profile/my-profile.component.ts @@ -33,6 +33,9 @@ export class MyProfileComponent implements OnInit { private recoverEmail:string; private user: User = new User(); + private updateSuccess:boolean; + private newPassword:string; + private incorrectPassword:boolean; private userPayment: UserPayment = new UserPayment(); private userBilling: UserBilling = new UserBilling(); private userPaymentList: UserPayment[] = []; @@ -119,6 +122,20 @@ export class MyProfileComponent implements OnInit { ); } + onUpdateUserInfo() { + this.userService.updateUserInfo(this.user, this.newPassword).subscribe( + res => { + console.log(res.text()); + this.updateSuccess = true; + }, + error => { + console.log(error.text()); + let errorMessage=error.text(); + if (errorMessage==="Incorrect current password!") this.incorrectPassword=true; + } + ); + } + onNewPayment () { this.paymentService.newPayment(this.userPayment).subscribe( res => { diff --git a/store-front/src/app/services/user.service.ts b/store-front/src/app/services/user.service.ts index 4d01174..609059e 100644 --- a/store-front/src/app/services/user.service.ts +++ b/store-front/src/app/services/user.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@angular/core'; import {AppConst} from '../constants/app-const'; import {Http, Headers} from '@angular/http'; +import {User} from '../models/User'; @Injectable() export class UserService { @@ -18,6 +19,25 @@ export class UserService { 'Content-Type': 'application/json', 'x-auth-token' : localStorage.getItem("xAuthToken") }); + return this.http.post(url, userInfo, {headers : tokenHeader}); + } + + updateUserInfo(user:User, newPassword:string) { + + let url = this.serverPath+"/user/updateUserInfo"; + let userInfo = { + "id" : user.id, + "firstName" : user.firstName, + "lastName" : user.lastName, + "username" : user.username, + "currentPassword" : user.password, + "email" : user.email, + "newPassword" : newPassword + }; + let tokenHeader = new Headers ({ + 'Content-Type': 'application/json', + 'x-auth-token' : localStorage.getItem("xAuthToken") + }); return this.http.post(url, JSON.stringify(userInfo), {headers : tokenHeader}); }