Skip to content

Commit

Permalink
Merge branch 'feature#188' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanvicente committed Feb 5, 2024
2 parents 5137514 + 8ab8052 commit d0d1e24
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 38 deletions.
9 changes: 1 addition & 8 deletions src/app/interceptors/jwt-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@ export class JwtInterceptor implements HttpInterceptor {
private authService: AuthService) { }

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

const currentUser = this.authService.currentUser;

console.log("~~~~~~~~~~~"+JSON.stringify(currentUser));

this.authService.user$.subscribe((user) => {
console.log("#########"+JSON.stringify(user));
});
const isApiUrl = request.url.startsWith(environment.BACKEND_ITA_CHALLENGE_BASE_URL);

return next.handle(request);
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export class User {
idUser: string;
dni?: string;
email?: string;
password?: string;
email?: string;
confirmPassword?: string;
itineraryId?: string;

constructor(
idUser: string,
dni?: string,
email?: string,
password?: string,
email?: string,
confirmPassword?: string,
itineraryId?: string
) {
Expand Down
27 changes: 15 additions & 12 deletions src/app/modules/starter/components/starter/starter.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {DataChallenge} from "../../../../models/data-challenge.model";
import {Challenge} from "../../../../models/challenge.model";
import { environment } from '../../../../../environments/environment';
import { FiltersModalComponent } from 'src/app/modules/modals/filters-modal/filters-modal.component';
import {AuthService} from "../../../../services/auth.service";
import {User} from "../../../../models/user.model";


@Component({
Expand All @@ -33,8 +35,9 @@ export class StarterComponent {

constructor(private activatedRoute: ActivatedRoute,
private router: Router,
private starterService: StarterService
) {
private starterService: StarterService,
private authService: AuthService
) {

this.params$ = this.activatedRoute.params.subscribe(params => {

Expand All @@ -54,16 +57,16 @@ export class StarterComponent {
getChallengesByPage(page: number) {
this.challengesSubs$ = this.starterService.getAllChallenges(page, this.pageSize).subscribe(resp => {

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ TODO DEVELOPMENT ONLY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
console.log('User Starter Component: ' + this.authService.currentUser.idUser);

// this.dataChallenge = new DataChallenge(resp);
// this.challenges = this.dataChallenge.challenges;
// this.numChallenges = this.challenges.length;
// this.totalPages = Math.ceil(this.numChallenges / this.pageSize);
if(this.authService.currentUser.idUser === 'anonym') {
const loggedUser: User = new User('', '32983483B', 'rU2GiuiTf3oj2RvQjMQX8EyozA7k2ehTp8YIUGSWOL3TdZcn7jaq7vG8z5ovfo6NMr77');
this.authService.login(loggedUser);
console.log(this.authService.currentUser.idUser);
}

// const startIndex = (page -1) * this.pageSize;
// const endIndex = startIndex + this.pageSize;
// this.listChallenges = this.challenges.slice(startIndex, endIndex);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
this.listChallenges = resp;
});
}
Expand All @@ -74,7 +77,7 @@ export class StarterComponent {
}

openModal() {
this.modalContent.open();
this.modalContent.open();
}

getChallengeFilters(filters: FilterChallenge){
Expand All @@ -87,5 +90,5 @@ export class StarterComponent {
//TODO: llamar al endpoint
}
}

}
50 changes: 43 additions & 7 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ import {
import { User } from "../models/user.model";
import { Router } from "@angular/router";

interface loginResponse {
id: string;
authToken: string;
refreshToken: string;
}

@Injectable()
export class AuthService {

private readonly anonym: string = "anonym";
private userSubject: BehaviorSubject<User>;
public user$: Observable<User>;

constructor(private http: HttpClient, private router: Router) {
constructor(private http: HttpClient,
private router: Router) {
this.userSubject = new BehaviorSubject(JSON.parse(localStorage.getItem("user")!));
this.user$ = this.userSubject.asObservable();
}
Expand All @@ -37,22 +44,51 @@ export class AuthService {
return this.userSubject.value;
}

public set currentUser(user: User) {
this.userSubject.next(user);
localStorage.setItem("user", JSON.stringify(user));
}

/**
* Register a user and log in with the new user. Set new user as current user.
*/
public register(dni: string, email:string, password: string, confirmPassword:string): User{



return this.currentUser;
public register(user:User): Observable<any>{

return this.http.post((environment.BACKEND_ITA_SSO_BASE_URL.concat(environment.BACKEND_SSO_REGISTER_URL)),
{
'dni': 'user.dni',
'password': user.password,
'confirmPassword':user.confirmPassword,
'email': user.email,
'itineraryId': user.itineraryId
},
{
headers: {
'Content-Type': 'application/json'
}
});
}


/**
* Log in with a user. Set user as current user.
*/
public login(dni: string, password: string) {
public login(user:User) {

this.http.post<loginResponse>(environment.BACKEND_ITA_SSO_BASE_URL.concat(environment.BACKEND_SSO_LOGIN_URL),
{
'dni': user.dni,
'password': user.password
},
{
headers: {
'Content-Type': 'application/json'
}
}).subscribe((resp: loginResponse) => {
this.currentUser = new User(resp.id);
localStorage.setItem("authToken", resp.authToken);
localStorage.setItem("refreshToken", resp.refreshToken);
});
}


Expand Down
2 changes: 1 addition & 1 deletion src/app/services/starter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class StarterService {
'Content-Type' : 'application/json'
})

return this.http.get(`${environment.BACKEND_ITA_CHALLENGE_BASE_URL}${environment.BACKEND_ALL_CHALLENGES}`,
return this.http.get(`${environment.BACKEND_ITA_CHALLENGE_BASE_URL}${environment.BACKEND_ALL_CHALLENGES_URL}`,
{
//params,
headers
Expand Down
8 changes: 4 additions & 4 deletions src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export const environment = {
appVersion: require('../../package.json').version,
BACKEND_ITA_CHALLENGE_BASE_URL: '/itachallenge/api/v1',
BACKEND_ITA_SSO_BASE_URL: 'https://dev.sso.itawiki.eurecatacademy.org/api/v1',
BACKEND_ALL_CHALLENGES: '/challenge/challenges',
BACKEND_SSO_REGISTER: '/auth/register',
BACKEND_SSO_LOGIN:'/auth/login',
BACKEND_SSO_VALIDATE_TOKEN:'/tokens/validate',
BACKEND_ALL_CHALLENGES_URL: '/challenge/challenges',
BACKEND_SSO_REGISTER_URL: '/auth/register',
BACKEND_SSO_LOGIN_URL:'/auth/login',
BACKEND_SSO_VALIDATE_TOKEN_URL:'/tokens/validate',
ITINERARY_ID: "clpb8t1cc000008k0cg1icvl4",
AUTHORIZATION: 'Authorization',
BEARER: 'Bearer ',
Expand Down
8 changes: 4 additions & 4 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export const environment = {
appVersion: require('../../package.json').version,
BACKEND_ITA_CHALLENGE_BASE_URL: '/itachallenge/api/v1',
BACKEND_ITA_SSO_BASE_URL: 'https://dev.sso.itawiki.eurecatacademy.org/api/v1',
BACKEND_ALL_CHALLENGES: '/challenge/challenges',
BACKEND_SSO_REGISTER: '/auth/register',
BACKEND_SSO_LOGIN:'/auth/login',
BACKEND_SSO_VALIDATE_TOKEN:'/tokens/validate',
BACKEND_ALL_CHALLENGES_URL: '/challenge/challenges',
BACKEND_SSO_REGISTER_URL: '/auth/register',
BACKEND_SSO_LOGIN_URL:'/auth/login',
BACKEND_SSO_VALIDATE_TOKEN_URL:'/tokens/validate',
ITINERARY_ID: "clpb8t1cc000008k0cg1icvl4",
AUTHORIZATION: 'Authorization',
BEARER: 'Bearer ',
Expand Down

0 comments on commit d0d1e24

Please sign in to comment.