Skip to content

Commit 44a0dd1

Browse files
committed
Create different environments for Staging & Production
1 parent 43a3e4d commit 44a0dd1

15 files changed

+124
-35
lines changed

.angular-cli.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"environments": {
2727
"dev": "environments/environment.ts",
2828
"hmr": "environments/environment.hmr.ts",
29+
"staging": "environments/environment.staging.ts",
2930
"prod": "environments/environment.prod.ts"
3031
}
3132
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Angular SPA Web API Changelog
22

3+
<a name="Nov 17, 2017"></a>
4+
### Nov 17, 2017
5+
* Create different environments for Staging & Production
6+
37
<a name="Nov 12-13, 2017"></a>
48
### Nov 12-13, 2017
59
* Switch to angular-oauth2-oidc

Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"applicationUrl": "http://localhost:5000/"
2727
},
28-
"AngularSPAWebAPI_Prod": {
28+
"AngularSPAWebAPI_Staging": {
2929
"commandName": "Project",
3030
"launchBrowser": true,
3131
"environmentVariables": {

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ you should use an OpenID Connect implicit flow.
2727
- Resource Owner Password Credentials grant
2828
- Refresh token
2929
- Role based Authorization
30+
- Development, Staging & Production environments
3031

3132
## Project structure
3233
The structure of the project is based on [Angular CLI ASP.NET Core](https://github.com/robisim74/AngularCliAspNetCore).
@@ -87,11 +88,15 @@ The structure of the project is based on [Angular CLI ASP.NET Core](https://gith
8788

8889
Make the changes to the Angular app: the browser will update without refreshing.
8990

90-
#### Staging / Production
91-
- `npm run build`
91+
#### Staging
92+
- `npm run build:staging`
9293
- Set _Staging_ as environment variable
9394
- `dotnet run --no-launch-profile`
9495

96+
#### Production
97+
- `npm run build:prod`
98+
- Publish
99+
95100
### Visual Studio 2017
96101

97102
#### Development
@@ -101,11 +106,15 @@ Make the changes to the Angular app: the browser will update without refreshing.
101106

102107
Make the changes to the Angular app: the browser will update without refreshing.
103108

104-
#### Staging / Production
105-
- `npm run build`
106-
- Select _AngularSPAWebAPI_Prod_ profile
109+
#### Staging
110+
- `npm run build:staging`
111+
- Select _AngularSPAWebAPI_Staging_ profile
107112
- Start debugging
108113

114+
#### Production
115+
- `npm run build:prod`
116+
- Publish
117+
109118
## Changing db
110119
To use another database:
111120
- Edit `ConnectionStrings` in _appsettings.json_

Startup.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ namespace AngularSPAWebAPI
1616
{
1717
public class Startup
1818
{
19-
public Startup(IConfiguration configuration)
19+
private readonly IHostingEnvironment currentEnvironment;
20+
21+
public Startup(IConfiguration configuration, IHostingEnvironment env)
2022
{
2123
Configuration = configuration;
24+
currentEnvironment = env;
2225
}
2326

2427
public IConfiguration Configuration { get; }
@@ -76,15 +79,28 @@ public void ConfigureServices(IServiceCollection services)
7679
.AddInMemoryClients(Config.GetClients())
7780
.AddAspNetIdentity<ApplicationUser>(); // IdentityServer4.AspNetIdentity.
7881

79-
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
80-
.AddIdentityServerAuthentication(options =>
81-
{
82-
options.Authority = "http://localhost:5000/";
83-
//options.Authority = "http://angularspawebapi.azurewebsites.net/";
84-
options.RequireHttpsMetadata = false;
85-
86-
options.ApiName = "WebAPI";
87-
});
82+
if (currentEnvironment.IsProduction())
83+
{
84+
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
85+
.AddIdentityServerAuthentication(options =>
86+
{
87+
options.Authority = "http://angularspawebapi.azurewebsites.net/";
88+
options.RequireHttpsMetadata = false;
89+
90+
options.ApiName = "WebAPI";
91+
});
92+
}
93+
else
94+
{
95+
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
96+
.AddIdentityServerAuthentication(options =>
97+
{
98+
options.Authority = "http://localhost:5000/";
99+
options.RequireHttpsMetadata = false;
100+
101+
options.ApiName = "WebAPI";
102+
});
103+
}
88104

89105
services.AddMvc();
90106
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"scripts": {
66
"ng": "ng",
77
"start": "ng serve --aot --proxy-config proxy.config.js --hmr -e=hmr",
8-
"build": "ng build --prod",
8+
"build:staging": "ng build --prod --env=staging",
9+
"build:prod": "ng build --prod",
910
"test": "ng test",
1011
"lint": "ng lint",
1112
"e2e": "ng e2e"

src/app/app.component.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,6 @@ export class AppComponent implements OnInit {
3030
private authenticationService: AuthenticationService,
3131
private router: Router
3232
) {
33-
// angular-oauth2-oidc configuration.
34-
this.oAuthService.clientId = "AngularSPA";
35-
this.oAuthService.scope = "openid offline_access WebAPI profile roles";
36-
this.oAuthService.setStorage(this.authenticationService.storage);
37-
//this.oAuthService.issuer = "http://angularspawebapi.azurewebsites.net";
38-
this.oAuthService.issuer = "http://localhost:5000";
39-
this.oAuthService.oidc = false;
40-
this.oAuthService.requireHttps = false;
41-
42-
//const url: string = 'http://angularspawebapi.azurewebsites.net/.well-known/openid-configuration';
43-
const url: string = 'http://localhost:5000/.well-known/openid-configuration';
44-
45-
// Loads Discovery Document.
46-
this.oAuthService.loadDiscoveryDocument(url);
47-
4833
if (this.oAuthService.hasValidAccessToken()) {
4934
this.authenticationService.init();
5035

src/app/app.module.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule } from '@angular/core';
1+
import { NgModule, APP_INITIALIZER } from '@angular/core';
22
import { BrowserModule, Title } from '@angular/platform-browser';
33
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
44

@@ -13,6 +13,11 @@ import { AppComponent } from './app.component';
1313
import { HomeComponent } from './home/home.component';
1414

1515
import { OAuthModule } from 'angular-oauth2-oidc';
16+
import { OAuthConfig } from './oauth.config';
17+
18+
export function initOAuth(oAuthConfig: OAuthConfig): Function {
19+
return () => oAuthConfig.load();
20+
}
1621

1722
@NgModule({
1823
imports: [
@@ -30,6 +35,13 @@ import { OAuthModule } from 'angular-oauth2-oidc';
3035
],
3136
providers: [
3237
Title,
38+
OAuthConfig,
39+
{
40+
provide: APP_INITIALIZER,
41+
useFactory: initOAuth,
42+
deps: [OAuthConfig],
43+
multi: true
44+
},
3345
AuthGuard,
3446
AuthenticationService,
3547
IdentityService

src/app/oauth.config.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Injectable } from '@angular/core';
2+
import { AuthConfig, OAuthService } from 'angular-oauth2-oidc';
3+
4+
import { environment } from '../environments/environment';
5+
6+
export const oAuthDevelopmentConfig: AuthConfig = {
7+
8+
clientId: "AngularSPA",
9+
scope: "openid offline_access WebAPI profile roles",
10+
oidc: false,
11+
issuer: "http://localhost:5000",
12+
requireHttps: false
13+
14+
}
15+
16+
export const oAuthProductionConfig: AuthConfig = {
17+
18+
clientId: "AngularSPA",
19+
scope: "openid offline_access WebAPI profile roles",
20+
oidc: false,
21+
issuer: "http://angularspawebapi.azurewebsites.net",
22+
requireHttps: false
23+
24+
}
25+
26+
/**
27+
* angular-oauth2-oidc configuration.
28+
*/
29+
@Injectable() export class OAuthConfig {
30+
31+
constructor(private oAuthService: OAuthService) { }
32+
33+
load(): Promise<object> {
34+
let url: string
35+
36+
if (environment.production) {
37+
// Production environment.
38+
this.oAuthService.configure(oAuthProductionConfig);
39+
url = 'http://angularspawebapi.azurewebsites.net/.well-known/openid-configuration';
40+
} else {
41+
// Development & Staging environments.
42+
this.oAuthService.configure(oAuthDevelopmentConfig);
43+
url = 'http://localhost:5000/.well-known/openid-configuration';
44+
}
45+
46+
// Defines the storage.
47+
this.oAuthService.setStorage(localStorage);
48+
49+
// Loads Discovery Document.
50+
return this.oAuthService.loadDiscoveryDocument(url);
51+
}
52+
53+
}

src/app/services/authentication.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { User } from '../models/user';
1717
*/
1818
@Injectable() export class AuthenticationService {
1919

20-
// Defines the storage.
20+
// As in OAuthConfig.
2121
public storage: Storage = localStorage;
2222

2323
/**

0 commit comments

Comments
 (0)