Last active
March 10, 2020 11:46
-
-
Save Sina7312/8c16d34aea3fe064c049b84285e02d29 to your computer and use it in GitHub Desktop.
Angular App Initializer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule, APP_INITIALIZER } from '@angular/core'; | |
import { BrowserModule } from '@angular/platform-browser'; | |
import { RouteReuseStrategy } from '@angular/router'; | |
import { HttpClientModule } from '@angular/common/http'; | |
import { IonicModule, IonicRouteStrategy } from '@ionic/angular'; | |
import { SplashScreen } from '@ionic-native/splash-screen/ngx'; | |
import { StatusBar } from '@ionic-native/status-bar/ngx'; | |
import { IonicStorageModule } from '@ionic/storage' | |
import { AppComponent } from './app.component'; | |
import { AppRoutingModule } from './app-routing.module'; | |
import { UserService } from './providers/user.service'; | |
import { SocketService } from './providers/socket.service'; | |
import { HttpService } from './providers/http.service'; | |
import { ImageModalComponent } from './components/image-modal/image-modal.component'; | |
import { PinchZoomModule } from 'ngx-pinch-zoom'; | |
import { OneSignalService } from './providers/onsignal.service'; | |
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; | |
import { Device } from '@ionic-native/device/ngx'; | |
import { OneSignal } from '@ionic-native/onesignal/ngx'; | |
import { OneSignalNativeService } from './providers/onsignal-native.service'; | |
import { Media } from '@ionic-native/media/ngx'; | |
import { File } from '@ionic-native/file/ngx'; | |
import { TabsPage } from './pages/tabs/tabs.page'; | |
import { TabsService } from './pages/tabs/tabs.service'; | |
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | |
export function userServiceFactory(userService: UserService): Function { | |
return () => userService.authenticate(); | |
} | |
@NgModule({ | |
declarations: [AppComponent, ImageModalComponent, TabsPage], | |
entryComponents: [ImageModalComponent], | |
imports: [ | |
BrowserModule, | |
IonicModule.forRoot(), | |
IonicStorageModule.forRoot(), | |
AppRoutingModule, | |
HttpClientModule, | |
PinchZoomModule, | |
], | |
providers: [ | |
StatusBar, | |
SplashScreen, | |
HttpService, | |
UserService, | |
SocketService, | |
OneSignalService, | |
OneSignalNativeService, | |
InAppBrowser, | |
Device, | |
OneSignal, | |
Media, | |
TabsService, | |
BrowserAnimationsModule, | |
File, | |
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }, | |
{ | |
provide: APP_INITIALIZER, | |
useFactory: userServiceFactory, | |
deps: [UserService], | |
multi: true | |
} | |
], | |
bootstrap: [AppComponent] | |
}) | |
export class AppModule { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; | |
import { environment } from '../../environments/environment'; | |
import { catchError, retry } from 'rxjs/operators'; | |
import { throwError } from 'rxjs'; | |
@Injectable() | |
export class HttpService { | |
DOMAIN = environment.domain; | |
headers; | |
constructor(private http: HttpClient) { } | |
/** | |
* Login with provided info and return token | |
* @param mobile | |
* @param password | |
*/ | |
login(mobile, password) { | |
return this.http.post<LoginInterface>(`${this.DOMAIN}/auth/local`, { mobile, password }); | |
} | |
/** | |
* Authenticate User token | |
* @param token | |
*/ | |
authenticate(token) { | |
this.headers = new HttpHeaders({ | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${token}` | |
}) | |
return this.http.get<UserInterface>(`${this.DOMAIN}/api/users/me`, { headers: this.headers }); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector } from '@angular/core'; | |
import { Storage } from '@ionic/storage'; | |
import { Router } from '@angular/router'; | |
import { HttpService, UserInterface } from './http.service'; | |
import { OneSignalService } from './onsignal.service'; | |
import { environment } from '../../environments/environment'; | |
import { OneSignalNativeService } from './onsignal-native.service'; | |
import { Device } from '@ionic-native/device/ngx'; | |
@Injectable() | |
export class UserService { | |
private _token: string; | |
private _userInfo: UserInterface; | |
TOKEN = 'token'; | |
constructor( | |
public storage: Storage, | |
private http: HttpService, | |
private injector: Injector, | |
private oneSignal: OneSignalService, | |
private oneSignalNative: OneSignalNativeService, | |
private device: Device | |
) { } | |
public get router(): Router { | |
return this.injector.get(Router); | |
} | |
async authenticate(): Promise<any> { | |
let token = await this.storage.get(this.TOKEN); | |
if (!token) return Promise.resolve(); | |
return this.http.authenticate(token).toPromise() | |
.then(res => { | |
this._token = token; | |
this._userInfo = res; | |
if (environment.production) this.device.cordova ? this.oneSignalNative.init() : this.oneSignal.init(); | |
}).catch(err => { | |
return Promise.resolve(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment