Skip to content

Commit 6dee4f1

Browse files
committed
✨ log out implemented
1 parent 569f034 commit 6dee4f1

File tree

19 files changed

+145
-32
lines changed

19 files changed

+145
-32
lines changed

src/app/auth/auth.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component, ElementRef, HostListener, OnDestroy, OnInit, ViewChild } fro
22
import { Subscription } from 'rxjs';
33

44
import { AuthService } from '@services/auth.service';
5+
import { IModalAuth } from '@interfaces/modal.interface';
56

67
@Component({
78
selector: 'app-auth',
@@ -25,10 +26,18 @@ export class AuthComponent implements OnInit, OnDestroy {
2526
}
2627

2728
ngOnInit(): void {
28-
this.authSubscription = this.authService.isAuthenticatedEmitter.subscribe(isAuth => !isAuth ? this.openModal() : '');
29+
this.authSubscription = this.authService.isAuthenticatedEmitter.subscribe(({ to, isAuth }) => !isAuth && to !== 'hide' ? this.openModal({to, isAuth}) : '');
2930
}
3031

31-
openModal(): void {
32+
openModal(options: IModalAuth): void {
33+
const { to } = options;
34+
if(to !== 'init') {
35+
this.showMore = false;
36+
this.showLogin = to === 'login' ? true : false;
37+
} else {
38+
this.showMore = true;
39+
this.showLogin = false;
40+
}
3241
this.bodyElement.classList.add('modal-open');
3342
this.modalAuth.nativeElement.classList.add('modal-open');
3443
this.modalOpen = true;

src/app/core/components/sidebar/sidebar.component.css

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ nav .profile {
154154
transition: 0.3s;
155155
width: 100%;
156156
transition: 0.5s;
157+
gap: 14px;
157158
}
158159

159160
.profile-details .box-image {
@@ -171,10 +172,9 @@ nav .profile {
171172
}
172173

173174
.profile-details .username {
174-
margin: 0 5px;
175175
flex-grow: 1;
176-
font-size: 14px;
177-
font-weight: 400;
176+
font-size: 16px;
177+
font-weight: 500;
178178
color: var(--bg-color-secondary);
179179
white-space: nowrap;
180180
opacity: 1;
@@ -196,6 +196,21 @@ nav .profile {
196196
font-size: 24px;
197197
}
198198

199+
.profile .btn-wrapper {
200+
margin-bottom: 1rem;
201+
width: 100%;
202+
display: flex;
203+
flex-direction: column;
204+
align-items: center;
205+
justify-content: center;
206+
gap: 1rem;
207+
}
208+
209+
.btn {
210+
max-width: 90%;
211+
border-radius: 40px;
212+
}
213+
199214
@media (max-width: 1280px) {
200215
nav {
201216
width: 80px;
@@ -237,6 +252,10 @@ nav .profile {
237252
.profile-details .box-image {
238253
justify-content: center;
239254
}
255+
256+
.profile .btn-wrapper {
257+
display: none;
258+
}
240259
}
241260

242261
@media (max-width: 768px) {

src/app/core/components/sidebar/sidebar.component.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@
3333
<span class="tooltip">Settings</span>
3434
</li>
3535
</ul>
36-
<div class="profile" *ngIf="userActive">
37-
<div routerLink="/settings" class="profile-details">
36+
37+
<div class="profile">
38+
<div class="btn-wrapper" *ngIf="!isAuthenticated">
39+
<button (click)="showModal('login')" class="btn btn-following btn-lg">Sign in</button>
40+
<button (click)="showModal('register')" class="btn btn-primary btn-lg">Sign on</button>
41+
</div>
42+
<div *ngIf="isAuthenticated" routerLink="/settings" class="profile-details">
3843
<figure class="box-image">
3944
<img
4045
src="https://images.unsplash.com/photo-1487412720507-e7ab37603c6f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1171&q=80"

src/app/core/components/sidebar/sidebar.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ export class SidebarComponent implements OnInit, AfterViewInit, OnDestroy {
3535

3636
ngOnInit(): void {
3737
this.isAuthenticated = this.authService.isAuthenticated();
38-
this.userActiveSubscription = this.authService.isAuthenticatedEmitter.subscribe(resp => {
39-
if(resp) {
38+
this.userActiveSubscription = this.authService.isAuthenticatedEmitter.subscribe(({isAuth}) => {
39+
if(isAuth) {
4040
this.setUserInfoActive();
4141
this.isAuthenticated = true;
42-
};
42+
} else {
43+
this.isAuthenticated = false;
44+
}
4345
});
4446
}
4547

@@ -52,4 +54,8 @@ export class SidebarComponent implements OnInit, AfterViewInit, OnDestroy {
5254
return (name && name?.length > 0) ? `${name} ${lastName || ''}` : email;
5355
}
5456

57+
showModal(to: 'login'|'register'): void {
58+
this.authService.showModalAuth(to);
59+
}
60+
5561
}

src/app/core/guards/is-auth.guard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class IsAuthGuard implements CanActivate {
1717
return this.authService.validateToken().pipe(tap(isAuthenticated => {
1818
if (!isAuthenticated) {
1919
this.router.navigateByUrl('/').then(() => {
20-
this.authService.showModalAuth();
20+
this.authService.showModalAuth('init');
2121
});
2222
}
2323
}));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IModalAuth {
2+
isAuth: boolean;
3+
to?: 'login'|'register'|'init'|'hide';
4+
}

src/app/core/services/auth.service.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { IResponseLogin } from '@interfaces/response.interface';
88
import Storage from "@utils/storage.util";
99
import { User } from '@models/user.model';
1010
import { SettingService } from './setting.service';
11+
import { IModalAuth } from '@interfaces/modal.interface';
1112

1213
const base_url = environment.base_url;
1314

@@ -16,8 +17,8 @@ const base_url = environment.base_url;
1617
})
1718
export class AuthService {
1819

19-
private userActive: User;
20-
public isAuthenticatedEmitter: EventEmitter<boolean> = new EventEmitter();
20+
private userActive: User | null;
21+
public isAuthenticatedEmitter: EventEmitter<IModalAuth> = new EventEmitter();
2122

2223
constructor(
2324
private http: HttpClient,
@@ -36,26 +37,32 @@ export class AuthService {
3637
return (this.userActive) ? true : false;
3738
}
3839

39-
showModalAuth(): void {
40+
showModalAuth(to: 'login'|'register'|'init'): void {
4041
const isAuth: boolean = this.userActive ? true : false;
41-
this.isAuthenticatedEmitter.emit(isAuth);
42+
this.isAuthenticatedEmitter.emit({ isAuth, to });
4243
}
4344

4445
get getUserActive(): User {
45-
return this.userActive;
46+
return this.userActive as User;
4647
}
4748

4849
login(data: ILogin): Observable<IResponseLogin> {
4950
const url = `${base_url}/auth/login`;
5051
return this.http.post<IResponseLogin>(url, data).pipe(map(resp => {
5152
this.setUserActiveInfo(resp);
52-
this.isAuthenticatedEmitter.emit(true);
53+
this.isAuthenticatedEmitter.emit({isAuth: true});
5354
const { user } = resp;
5455
this.settingService.setTheme(user.darkMode ? 'dark' : 'light');
5556
return resp;
5657
}));
5758
}
5859

60+
logout(): void {
61+
Storage.deleteSessionStorage('x-token');
62+
this.userActive = null;
63+
this.isAuthenticatedEmitter.emit({ isAuth: false, to: 'hide' });
64+
}
65+
5966
validateToken() {
6067
const url = `${base_url}/auth/renew`;
6168
return this.http.get<IResponseLogin>(url, this.headers).pipe(map(resp => {
@@ -68,21 +75,23 @@ export class AuthService {
6875
Storage.deleteSessionStorage('x-token');
6976
Storage.saveSessionStorage('x-token', resp.token);
7077
this.userActive = resp.user;
71-
this.showModalAuth();
78+
this.showModalAuth('init');
7279
}
7380

7481
signIn(data: ILogin): Observable<Boolean> {
7582
const url = `${base_url}/user`;
7683
return this.http.post<IResponseLogin>(url, data).pipe(map(resp => {
7784
this.setUserActiveInfo(resp);
78-
this.isAuthenticatedEmitter.emit(true);
85+
this.isAuthenticatedEmitter.emit({isAuth: true});
7986
return resp.ok;
8087
}))
8188
}
8289

8390
setNewUserInfo(name: string, lastName: string): void {
84-
this.userActive.name = name;
85-
this.userActive.lastName = lastName
91+
if(this.userActive) {
92+
this.userActive.name = name;
93+
this.userActive.lastName = lastName
94+
}
8695
}
8796

8897
}

src/app/features/feed/pages/feed/feed.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class FeedComponent implements OnInit {
5858

5959
saveFeed(id: string) {
6060
if(!this.authService.isAuthenticated()) {
61-
return this.authService.showModalAuth();
61+
return this.authService.showModalAuth('init');
6262
}
6363
this.saveFeedSub.next(id);
6464
}

src/app/features/home/components/websites-card/websites-card.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class WebsitesCardComponent implements OnInit {
2626

2727
subscribeWebsite(id: string){
2828
if(!this.isAuthenticated) {
29-
return this.authService.showModalAuth();
29+
return this.authService.showModalAuth('init');
3030
}
3131
this.userService.modifyPreferences(id, 'subscription').subscribe(resp => {
3232
this.websites.map(website => {

src/app/features/home/home.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class HomeComponent implements OnInit, OnDestroy {
4242
ngOnInit(): void {
4343
this.isAuthenticated = this.authService.isAuthenticated();
4444
this.loadingNews.next(true);
45-
this.isAuthenticatedSub = this.authService.isAuthenticatedEmitter.subscribe(resp => resp ? this.resetDataInitial(): '');
45+
this.isAuthenticatedSub = this.authService.isAuthenticatedEmitter.subscribe(({isAuth}) => isAuth ? this.resetDataInitial(): '');
4646
}
4747

4848
resetDataInitial() {
@@ -92,9 +92,9 @@ export class HomeComponent implements OnInit, OnDestroy {
9292
}
9393

9494
onScroll() {
95-
this.skip += this.limit;
95+
this. skip += this.limit;
9696
if(!this.isAuthenticated && this.skip >= this.limit) {
97-
return this.authService.showModalAuth();
97+
return this.authService.showModalAuth('init');
9898
}
9999
this.getFeeds().subscribe(feeds => this.feeds = [...this.feeds, ...feeds]);
100100
}

0 commit comments

Comments
 (0)