Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class UserService {
- /**
- * Validate user's password - returns true if validated
- * Used in register and updatePassword functions
- */
- passwordValidation(pass: string) : boolean {
- if (pass.length >= 8 && /\d/.test(pass) && /[a-z]/.test(pass) && /[A-Z]/.test(pass))
- return true;
- else return false;
- }
- /**
- * Represents observer currently of logged user
- */
- loggedUser: Subject<User>;
- /**
- * Http headers and options used to create http requests
- */
- headers: Headers;
- options: RequestOptions;
- constructor(private http: Http) {
- this.loggedUser = new Subject<User>();
- this.headers = new Headers({'Content-Type': 'application/json'});
- this.options = new RequestOptions({headers: this.headers});
- }
- /**
- * Logs in existing user into selfiemap app
- * @param username Name of user
- * @param password Password of user
- */
- login(username: string, password: string) {
- let body = JSON.stringify({username, password});
- this.http.post(API_USER_LOGIN, body, this.options)
- .map(data => data.json()).subscribe((user) => {
- this.loggedUser.next(user);
- })
- }
- /**
- * Registers new user into selfiemap app
- * @param username Name of user
- * @param password Password of user
- * @param email E-mail of user
- * @param birthDate Birth date of user
- */
- register(username: string, password: string, email: string, birthDate: Date) {
- if (this.passwordValidation(password)) {
- let body = JSON.stringify({username, password, email, birthDate});
- this.http.post(API_USER_REGISTER, body, this.options)
- .map(data => data.json()).subscribe((user) => {
- this.loggedUser.next(user);
- });
- }
- else console.log('Bad password')
- }
- /**
- * Update password of existing user
- * @param oldPassword Old password to verify ownership of account
- * @param newPassword New password
- */
- updatePassword(oldPassword: string, newPassword: string) {
- if (this.passwordValidation(newPassword)) {
- let body = JSON.stringify({oldPassword, newPassword});
- this.http.put(API_USER_CHANGEPASSWORD, body, this.options)
- .map(data => data.json()).subscribe((user) => {
- this.loggedUser.next(user);
- });
- }
- else console.log('Bad password');
- }
- /**
- * Change user's language of seliemap app
- * @param language Language id (sk,cz,en,...)
- */
- setLanguage(language: string) {
- let body = JSON.stringify({language});
- this.http.put(API_USER_LANGUAGE, body, this.options)
- .map(data => data.json()).subscribe((user) => {
- this.loggedUser.next(user);
- });
- }
- /**
- * Change privacy of user's account
- * @param privateProfile If true, user's profile is private, if false, user's profile is public
- */
- setPrivacy(privateProfile: boolean) {
- let body = JSON.stringify({privateProfile});
- this.http.put(API_USER_PRIVACY, body, this.options)
- .map(data => data.json()).subscribe((user) => {
- this.loggedUser.next(user);
- });
- }
- /**
- * Get last known location of user
- * @returns {string} Last location
- */
- getLastLocation() {
- return '';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement