Last active
October 25, 2016 20:08
-
-
Save jhades/4d9e1422b020b10acaf616d3ee4a3db7 to your computer and use it in GitHub Desktop.
Angular Universal
This file contains 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 * as gulp from 'gulp'; | |
import {gulpPrerender} from '@angular/universal'; | |
import {App} from './app'; | |
gulp.task("prerender", () => { | |
return gulp.src(['index.html']) | |
.pipe(gulpPrerender({ | |
directives: [App] | |
})); | |
}); | |
This file contains 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
aws s3 cp prerendered/*.html s3://your-amazon-s3-bucket/your-site --recursive | |
--expires 2100-01-01T00:00:00Z --acl public-read | |
--cache-control max-age=2592000,public --content-encoding gzip |
This file contains 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 {createEngine} from 'angular2-express-engine'; | |
import { UniversalModule } from 'angular2-universal'; | |
app.engine('.html', createEngine({})); | |
app.set('views', __dirname); | |
app.set('view engine', 'html'); | |
@NgModule({ | |
bootstrap: [ App ], | |
declarations: [ App, ... ], | |
imports: [ | |
UniversalModule, // NodeModule, NodeHttpModule, and NodeJsonpModule are included | |
... | |
], | |
providers: [...], | |
}) | |
export class MainModule { | |
} | |
function ngApp(req, res) { | |
let baseUrl = '/'; | |
let url = req.originalUrl || '/'; | |
let config = { | |
req, | |
res, | |
requestUrl: url, | |
baseUrl, | |
ngModule: MainModule, | |
preboot: false, | |
originUrl: ROOT_URL | |
}; | |
console.log("rendering ..."); | |
res.render('index', config, (err, html) => { | |
res.status(200).send(html); | |
}); | |
} | |
This file contains 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
export interface AuthenticationService { | |
signUp(activationLink:boolean); | |
login(); | |
logout(); | |
isLoggedIn(); | |
isLoggedInWithGitHub(); | |
} | |
This file contains 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
export class ServerAuthenticationService implements AuthenticationService { | |
constructor(private authenticationId, private isUserLoggedIn:boolean) { | |
} | |
signUp() { | |
throw new Error("Signup event cannot be called while doing server side rendering"); | |
} | |
login() { | |
throw new Error("Login event cannot be called while doing server side rendering"); | |
} | |
logout() { | |
throw new Error("Logout event cannot be called while doing server side rendering"); | |
} | |
isLoggedIn() { | |
return this.isUserLoggedIn; | |
} | |
isLoggedInWithGitHub() { | |
... | |
} | |
} | |
This file contains 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
@Injectable() | |
export class ClientAuthenticationService implements AuthenticationService { | |
signUp(activationLink = false) { | |
this.messagesService.clearAll(); | |
const lock = new Auth0Lock(AUTH0_API_KEY, AUTH0_SUB_DOMAIN); | |
lock.showSignup(this.loginConfig(activationLink), this.loginCallback()); | |
} | |
.... | |
} | |
This file contains 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
// define an injection name | |
export const authenticationService = new OpaqueToken("authenticationService"); | |
// inject on the server | |
provide(authenticationService, { | |
useFactory: () => new ServerAuthenticationService(req.authenticationId,isLoggedIn) | |
}) | |
// inject on the client | |
provide(authenticationService, { | |
useFactory: (messagesService: MessagesService, ngZone: NgZone, cookieService: CookieService, userService: UserService, router:Router, gitHubService : GitHubService) => { | |
return new ClientAuthenticationService(messagesService, ngZone, cookieService, userService, router, gitHubService); | |
}, | |
deps: [MessagesService, NgZone, CookieService, UserService, Router, GitHubService] | |
})]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment