Skip to content

Commit cc4c8cc

Browse files
Initial commit
0 parents  commit cc4c8cc

40 files changed

+11614
-0
lines changed

.angular-cli.json

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
3+
"project": {
4+
"name": "angular-src"
5+
},
6+
"apps": [
7+
{
8+
"root": "src",
9+
"outDir": "dist",
10+
"assets": [
11+
"assets",
12+
"favicon.ico"
13+
],
14+
"index": "index.html",
15+
"main": "main.ts",
16+
"polyfills": "polyfills.ts",
17+
"test": "test.ts",
18+
"tsconfig": "tsconfig.app.json",
19+
"testTsconfig": "tsconfig.spec.json",
20+
"prefix": "app",
21+
"styles": [
22+
"styles.css"
23+
],
24+
"scripts": [],
25+
"environmentSource": "environments/environment.ts",
26+
"environments": {
27+
"dev": "environments/environment.ts",
28+
"prod": "environments/environment.prod.ts"
29+
}
30+
},
31+
{
32+
"root": "src",
33+
"outDir": "dist-server/",
34+
"assets": [
35+
"assets",
36+
"favicon.ico"
37+
],
38+
"index": "index.html",
39+
"main": "main.server.ts",
40+
"polyfills": "polyfills.ts",
41+
"test": "test.ts",
42+
"tsconfig": "tsconfig.server.json",
43+
"testTsconfig": "tsconfig.spec.json",
44+
"prefix": "app",
45+
"styles": [
46+
"styles.css"
47+
],
48+
"scripts": [],
49+
"environmentSource": "environments/environment.ts",
50+
"environments": {
51+
"dev": "environments/environment.ts",
52+
"prod": "environments/environment.prod.ts"
53+
},
54+
"platform": "server",
55+
"name": "universal"
56+
}
57+
],
58+
"e2e": {
59+
"protractor": {
60+
"config": "./protractor.conf.js"
61+
}
62+
},
63+
"lint": [
64+
{
65+
"project": "src/tsconfig.app.json",
66+
"exclude": "**/node_modules/**"
67+
},
68+
{
69+
"project": "src/tsconfig.spec.json",
70+
"exclude": "**/node_modules/**"
71+
},
72+
{
73+
"project": "e2e/tsconfig.e2e.json",
74+
"exclude": "**/node_modules/**"
75+
}
76+
],
77+
"test": {
78+
"karma": {
79+
"config": "./karma.conf.js"
80+
}
81+
},
82+
"defaults": {
83+
"styleExt": "css",
84+
"component": {}
85+
}
86+
}

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

.gitignore

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# compiled output
4+
/dist
5+
/dist-server
6+
/tmp
7+
/out-tsc
8+
9+
# dependencies
10+
/node_modules
11+
12+
# IDEs and editors
13+
/.idea
14+
.project
15+
.classpath
16+
.c9/
17+
*.launch
18+
.settings/
19+
*.sublime-workspace
20+
21+
# IDE - VSCode
22+
.vscode/*
23+
!.vscode/settings.json
24+
!.vscode/tasks.json
25+
!.vscode/launch.json
26+
!.vscode/extensions.json
27+
28+
# misc
29+
/.sass-cache
30+
/connect.lock
31+
/coverage
32+
/libpeerconnection.log
33+
npm-debug.log
34+
testem.log
35+
/typings
36+
37+
# e2e
38+
/e2e/*.js
39+
/e2e/*.map
40+
41+
# System Files
42+
.DS_Store
43+
Thumbs.db

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Stanza987
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# MEAN Stack App with Angular Universal
2+
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.6.2, and **can be used as a template for your MEAN stack projects!**
3+
4+
## Versions
5+
* MongoDB v3.6.0
6+
* Express v4.16.2
7+
* Angular v5.0.0
8+
* Node.js v9.3.0
9+
10+
## Background
11+
This repository contains code that was originally developed using the [MEAN Stack Front to Back](https://www.youtube.com/watch?v=uONz0lEWft0&list=PLillGF-RfqbZMNtaOXJQiDebNXjVapWPZ) video series by *Brad Traversy* and represents parts 1-4 of the 10 part series. During these videos, we have set up our back-end with Node.js/Express, our database with Mongoose/MongoDB, and have implemented API authentication/authorization using passport and a JSON web token (JWT) strategy. We are now ready to set up our front-end with Angular in [Part 5](https://www.youtube.com/watch?v=zrViDpWiNVE&t=5s).
12+
13+
## What's New?
14+
This project includes the original code as described above as well as the starter app generated from the Angular CLI using `ng new`. The Angular starter app has been linked to our Express/Node.js/MongoDB back-end via a single `server.js` file. **Angular Universal has also been integrated into the project for server-side rendering, making your MEAN stack project SEO and social media friendly.**
15+
16+
## Starting the Project
17+
To begin working with this project, perform the following tasks:
18+
19+
1. Clone the repository to your local machine
20+
2. Start up your local instance of MongoDB
21+
3. Run `npm install` to download dependencies
22+
4. Run `npm run build` to generate the */dist* folder for your Angular front-end and the */dist-server* folder for Angular Universal
23+
5. Run `node server.js` to start the server on `http://localhost:3000/`.
24+
25+
## Development server
26+
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
27+
28+
## Further help
29+
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).

config/database.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
database: 'mongodb://localhost:27017/meanauth',
3+
secret: 'my-secret-phrase'
4+
}

config/passport.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const JwtStrategy = require('passport-jwt').Strategy;
2+
const ExtractJwt = require('passport-jwt').ExtractJwt;
3+
const User = require('../models/user');
4+
const config = require('../config/database');
5+
6+
module.exports = (passport) => {
7+
let opts = {};
8+
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
9+
opts.secretOrKey = config.secret;
10+
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
11+
User.getUserById(jwt_payload.data._id, (err, user) => {
12+
if (err) return done(err, false);
13+
if (user) { return done(null, user); }
14+
else { return done(null, false); }
15+
});
16+
}))
17+
}

e2e/app.e2e-spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AppPage } from './app.po';
2+
3+
describe('angular-src App', () => {
4+
let page: AppPage;
5+
6+
beforeEach(() => {
7+
page = new AppPage();
8+
});
9+
10+
it('should display welcome message', () => {
11+
page.navigateTo();
12+
expect(page.getParagraphText()).toEqual('Welcome to app!');
13+
});
14+
});

e2e/app.po.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { browser, by, element } from 'protractor';
2+
3+
export class AppPage {
4+
navigateTo() {
5+
return browser.get('/');
6+
}
7+
8+
getParagraphText() {
9+
return element(by.css('app-root h1')).getText();
10+
}
11+
}

e2e/tsconfig.e2e.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../out-tsc/e2e",
5+
"baseUrl": "./",
6+
"module": "commonjs",
7+
"target": "es5",
8+
"types": [
9+
"jasmine",
10+
"jasminewd2",
11+
"node"
12+
]
13+
}
14+
}

karma.conf.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Karma configuration file, see link for more information
2+
// https://karma-runner.github.io/1.0/config/configuration-file.html
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '',
7+
frameworks: ['jasmine', '@angular/cli'],
8+
plugins: [
9+
require('karma-jasmine'),
10+
require('karma-chrome-launcher'),
11+
require('karma-jasmine-html-reporter'),
12+
require('karma-coverage-istanbul-reporter'),
13+
require('@angular/cli/plugins/karma')
14+
],
15+
client:{
16+
clearContext: false // leave Jasmine Spec Runner output visible in browser
17+
},
18+
coverageIstanbulReporter: {
19+
reports: [ 'html', 'lcovonly' ],
20+
fixWebpackSourcePaths: true
21+
},
22+
angularCli: {
23+
environment: 'dev'
24+
},
25+
reporters: ['progress', 'kjhtml'],
26+
port: 9876,
27+
colors: true,
28+
logLevel: config.LOG_INFO,
29+
autoWatch: true,
30+
browsers: ['Chrome'],
31+
singleRun: false
32+
});
33+
};

models/user.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const mongoose = require('mongoose');
2+
const bcrypt = require('bcryptjs');
3+
const config = require('../config/database');
4+
5+
// Initalize Schema
6+
const Schema = mongoose.Schema
7+
8+
// Define a UserSchema
9+
const UserSchema = new Schema({
10+
name: String,
11+
email: {
12+
type: String,
13+
required: true
14+
},
15+
username: {
16+
type: String,
17+
required: true
18+
},
19+
password: {
20+
type: String,
21+
required: true
22+
}
23+
});
24+
25+
// Define and Export Model
26+
const User = mongoose.model('User', UserSchema);
27+
module.exports = User;
28+
29+
// Query Functions
30+
module.exports.getUserById = (id, callback) => {
31+
User.findById(id, callback);
32+
}
33+
34+
module.exports.getUserByUsername = (username, callback) => {
35+
const query = { username }
36+
User.findOne(query, callback);
37+
}
38+
39+
module.exports.addUser = (newUser, callback) => {
40+
bcrypt.genSalt(10)
41+
.then((salt) => bcrypt.hash(newUser.password, salt))
42+
.then((hash) => {
43+
newUser.password = hash;
44+
newUser.save(callback);
45+
}).catch((err) => console.log('There was an error adding a user.'));
46+
}
47+
48+
module.exports.comparePassword = (candidatePassword, hash, callback) => {
49+
bcrypt.compare(candidatePassword, hash)
50+
.then((isMatch) => {
51+
callback(null, isMatch);
52+
}).catch((err) => console.log('There was an error with authentication.'));
53+
}

0 commit comments

Comments
 (0)