Skip to content

Commit e874f2d

Browse files
authored
Version 13 (#3)
* update AuthGuard * upograde auth module * set default data context config
1 parent 70012b9 commit e874f2d

12 files changed

Lines changed: 84 additions & 72 deletions

README.md

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Choose the version corresponding to your Angular version:
1919

2020
Angular | @themost/angular |
2121
--- | --- |
22+
13 | 13.x+
2223
12 | 12.x+
2324
11 | 11.x+
2425
6 | 6.x+
@@ -28,54 +29,62 @@ Angular | @themost/angular |
2829

2930
### Usage
3031

31-
Provide `DATA_CONTEXT_CONFIG` and set [`@themost-framework`](https://github.com/themost-framework) api server URI. Include `AngularDataContext` in application providers.
32+
Import `MostModule` by using `MostModule.forRoot()` and configure `AngularDataContext`:
3233

3334
app.module.ts
3435

3536
import { NgModule } from '@angular/core';
36-
import { HttpClientModule } from '@angular/common/http';
3737
import { BrowserModule } from '@angular/platform-browser';
38+
import { AuthModule, MostModule } from '@themost/angular';
3839
import { AppComponent } from './app.component';
39-
import {DATA_CONTEXT_CONFIG, AngularDataContext } from '@themost/angular';
40+
4041
@NgModule({
41-
imports: [
42-
BrowserModule,
43-
HttpClientModule
44-
],
45-
providers: [
46-
{
47-
provide: DATA_CONTEXT_CONFIG, useValue: {
48-
base: 'http://localhost:3000/'
49-
}
50-
},
51-
AngularDataContext
52-
],
53-
declarations: [AppComponent],
54-
bootstrap: [AppComponent],
42+
declarations: [
43+
AppComponent
44+
],
45+
imports: [
46+
BrowserModule,
47+
...
48+
MostModule.forRoot({
49+
base: '/api/'
50+
}),
51+
AuthModule.forRoot({
52+
client_id: '9165351833584149',
53+
scope: [
54+
'profile'
55+
],
56+
locations: [],
57+
login: '/auth/login',
58+
callback: '/client/callback'
59+
})
60+
],
61+
providers: [],
62+
bootstrap: [AppComponent]
5563
})
5664
export class AppModule { }
5765

58-
app.component.ts
66+
index.component.ts
67+
68+
import { Component, OnInit } from '@angular/core';
69+
import { AngularDataContext } from '@themost/angular';
70+
import { from } from 'rxjs';
5971

60-
import { Component } from '@angular/core';
61-
import {AngularDataContext} from "@themost/angular";
6272
@Component({
63-
selector: 'test-app',
64-
templateUrl: 'app.component.html'
73+
selector: 'app-index',
74+
templateUrl: './index.component.html',
75+
styleUrls: ['./index.component.scss']
6576
})
66-
export class AppComponent {
67-
public laptops:any[];
68-
constructor(private context:AngularDataContext) {
69-
this.context.setBasicAuthorization('alexis.rees@example.com','user');
70-
}
71-
72-
ngOnInit() {
73-
.where('category').equal('Laptops')
74-
.orderBy('price')
75-
.take(5)
76-
.getItems().then((result)=> {
77-
this.laptops = result;
78-
});
77+
export class IndexComponent implements OnInit {
78+
items$: any;
79+
80+
constructor(private context: AngularDataContext) { }
81+
82+
ngOnInit(): void {
83+
this.items$ = from(this.context.model('Products')
84+
.where((x:any) => x.category === 'Laptops')
85+
.orderBy((x:any) => x.price)
86+
.take(5)
87+
.getItems());
7988
}
8089
}
8190

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"name": "@themost/angular",
3-
"version": "12.1.0",
3+
"version": "13.1.0",
44
"description": "MOST Web Framework client module for angular",
55
"main": "./bundles/themost-angular.umd.js",
66
"scripts": {
77
},
88
"peerDependencies": {
9-
"@angular/common": "^12",
10-
"@angular/core": "^12",
11-
"@angular/platform-browser": "^12",
12-
"@angular/platform-browser-dynamic": "^12",
13-
"@themost/client": "^2.9.0",
14-
"@themost/xml": "^2.5.2",
9+
"@angular/common": "^13",
10+
"@angular/core": "^13",
11+
"@angular/platform-browser": "^13",
12+
"@angular/platform-browser-dynamic": "^13",
13+
"@themost/client": "^2",
14+
"@themost/xml": "^2",
1515
"core-js": "^3.21.1",
16-
"rxjs": "^6.6.0",
16+
"rxjs": "~7.5.0",
1717
"zone.js": "^0.11.4"
1818
},
1919
"repository": {

src/auth/activated-user.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Injectable } from '@angular/core';
33
import { AngularDataContext } from '../client';
44
import { BehaviorSubject } from 'rxjs';
55

6-
@Injectable()
76
export class ActivatedUserSnapshot {
87
private static readonly StorageKey = 'ActivatedUserSnapshot.activatedUser';
98
constructor(private context: AngularDataContext) {
@@ -32,10 +31,12 @@ export class ActivatedUserSnapshot {
3231
@Injectable()
3332
export class ActivatedUserService {
3433

35-
public user: BehaviorSubject<any> = new BehaviorSubject(this.activatedUser);
36-
37-
constructor(private context: AngularDataContext, public snapshot: ActivatedUserSnapshot) {
34+
public user: BehaviorSubject<any>;
35+
public snapshot: ActivatedUserSnapshot;
36+
37+
constructor(private context: AngularDataContext) {
3838
this.snapshot = new ActivatedUserSnapshot(context);
39+
this.user = new BehaviorSubject(this.activatedUser);
3940
}
4041

4142
private get activatedUser() {

src/auth/auth.guard.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Router, Routes } from '@angular/router';
33
import { RouterTestingModule } from '@angular/router/testing';
44
import { MostModule } from '../module';
55
import { ActivatedUserService } from './activated-user.service';
6-
import { AuthGuard } from './auth.guard';
76
import { AUTH_CONFIG } from './auth.interfaces';
87
import { AuthModule } from './auth.module';
98
export const routes: Routes = [];

src/auth/auth.guard.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// MOST Web Framework Codename Zero Gravity Copyright (c) 2017-2022, THEMOST LP All rights reserved
22
import { Inject, Injectable } from '@angular/core';
3-
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, Router } from '@angular/router';
3+
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, Router, UrlTree } from '@angular/router';
44
import { Observable } from 'rxjs';
55
import { AppLocation, AppLocationMask, AppLocationPattern } from './auth.interfaces';
66
import { ActivatedUserService } from './activated-user.service';
@@ -27,6 +27,13 @@ export class AuthGuard implements CanActivate, CanActivateChild {
2727

2828
public canActivateLocation(path: string, user: any): AppLocation {
2929
let accounts = [];
30+
if (this.locations && this.locations.length === 0) {
31+
const mask = 1;
32+
return {
33+
path,
34+
mask
35+
}
36+
}
3037
if (user && user.groups) {
3138
accounts = user.groups.map((x: any) => {
3239
return x.name;
@@ -47,7 +54,7 @@ export class AuthGuard implements CanActivate, CanActivateChild {
4754
}
4855

4956
private _canActivate(route: ActivatedRouteSnapshot,
50-
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
57+
state: RouterStateSnapshot): boolean {
5158
const activatedLocation = this.canActivateLocation(state.url, this.activatedUser.snapshot.user);
5259
let result = false;
5360
if (activatedLocation != null) {
@@ -60,13 +67,11 @@ export class AuthGuard implements CanActivate, CanActivateChild {
6067
return result;
6168
}
6269

63-
canActivate(
64-
next: ActivatedRouteSnapshot,
65-
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
70+
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
6671
return this._canActivate(next, state);
6772
}
6873

69-
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
74+
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
7075
return this._canActivate(childRoute, state);
7176
}
7277
}

src/auth/auth.module.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AuthRoutingModule } from './auth-routing.module';
99
import { CallbackComponent } from './callback.component';
1010
import { LoginComponent } from './login.component';
1111
import { LogoutComponent } from './logout.component';
12-
import { ActivatedUserService, ActivatedUserSnapshot } from './activated-user.service';
12+
import { ActivatedUserService } from './activated-user.service';
1313
import { MostModule } from '../module';
1414

1515

@@ -34,10 +34,9 @@ export class AuthModule {
3434
provide: AUTH_CONFIG,
3535
useValue: config
3636
},
37-
AuthGuard,
38-
AuthService,
39-
ActivatedUserSnapshot,
4037
ActivatedUserService,
38+
AuthGuard,
39+
AuthService
4140
]
4241
};
4342
}

src/auth/callback.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class User {
1414
}
1515

1616
@Component({
17-
selector: 'app-callback',
17+
selector: 'login-callback',
1818
template: `
1919
<div></div>
2020
`
@@ -45,7 +45,6 @@ export class CallbackComponent implements OnInit, OnDestroy {
4545
}
4646
});
4747
this.activatedUser.set(authorizedUser);
48-
this.activatedUser.user.next(authorizedUser);
4948
return this.router.navigate([ '/' ]);
5049
});
5150
});

src/auth/login.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Subscription } from 'rxjs';
44
import { AuthService } from './auth.service';
55

66
@Component({
7-
selector: 'app-logout',
7+
selector: 'user-login',
88
template: `
99
<div></div>
1010
`

src/auth/logout.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Subscription } from 'rxjs';
44
import { AuthService } from './auth.service';
55

66
@Component({
7-
selector: 'app-login',
7+
selector: 'user-logout',
88
template: `
99
<div></div>
1010
`

src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ClientDataService, ClientDataContext, Args, DataServiceExecuteOptions,
66

77
export interface ClientDataContextConfig {
88
base: string;
9-
options: ClientDataContextOptions;
9+
options?: ClientDataContextOptions;
1010
}
1111

1212
export const DATA_CONTEXT_CONFIG = new InjectionToken<ClientDataContextConfig>('data.config');

0 commit comments

Comments
 (0)