From 02bffcd04a0d82792070e841059960132a401268 Mon Sep 17 00:00:00 2001 From: Tmadkaud Date: Thu, 17 May 2018 22:22:09 +0200 Subject: [PATCH] code cecilia et thierry --- auth/facebook.js | 17 +++++++ auth/github.js | 16 +++++++ auth/google.js | 16 +++++++ models/FileMongo.js | 2 +- package-lock.json | 72 ++++++++++++++++++++++++++++++ package.json | 3 ++ routes/api.js | 48 ++++++++++++++++++++ src/app/book/book.component.html | 22 ++++----- src/app/book/book.component.ts | 62 ++++++++++++++++++------- src/app/login/login.component.html | 6 +++ 10 files changed, 236 insertions(+), 28 deletions(-) create mode 100644 auth/facebook.js create mode 100644 auth/github.js create mode 100644 auth/google.js diff --git a/auth/facebook.js b/auth/facebook.js new file mode 100644 index 0000000..cfa5e38 --- /dev/null +++ b/auth/facebook.js @@ -0,0 +1,17 @@ +var passport = require('passport'); +var FacebookStrategy = require('passport-facebook').Strategy; + +passport.use(new FacebookStrategy({ + clientID: "191092458179642", + clientSecret: "964e490d7e49a13ee7a77c9b65419fb3", + callbackURL: "https://localhost:3000/auth/facebook/callback" + }, + function(accessToken, refreshToken, profile, done) { + User.findOrCreate({name: profile.displayName}, {name: profile.displayName,userid: profile.id}, function(err, user) { + if (err) { return done(err); } + done(null, user); + }); + } +)); + +module.exports = passport; \ No newline at end of file diff --git a/auth/github.js b/auth/github.js new file mode 100644 index 0000000..5738a84 --- /dev/null +++ b/auth/github.js @@ -0,0 +1,16 @@ +var passport = require('passport'); +var GitHubStrategy = require('passport-github').Strategy; + +passport.use(new GitHubStrategy({ + clientID: "60a0296da9fce6ca3b7e", + clientSecret: "9fb2f52123d9d840c98b98b3b4ffbc5dd77d1de3", + callbackURL: "https://localhost:3000/auth/github/callback" + }, + function(accessToken, refreshToken, profile, done) { + User.findOrCreate({userid: profile.id}, {name: profile.displayName,userid: profile.id}, function (err, user) { + return done(err, user); + }); + } +)); + +module.exports = passport; \ No newline at end of file diff --git a/auth/google.js b/auth/google.js new file mode 100644 index 0000000..a69bde0 --- /dev/null +++ b/auth/google.js @@ -0,0 +1,16 @@ +var passport = require('passport'); +var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; + +passport.use(new GoogleStrategy({ + clientID: "633754977454-7dcfrsfob7sjq5lim47tc63pdjch6imr.apps.googleusercontent.com", + clientSecret: "D7lHcUgZbCpmWZtooRlICzAH", + callbackURL: "https://localhost:3000/auth/google/callback" + }, + function(accessToken, refreshToken, profile, done) { + User.findOrCreate({ userid: profile.id }, { name: profile.displayName,userid: profile.id }, function (err, user) { + return done(err, user); + }); + } +)); + +module.exports = passport; \ No newline at end of file diff --git a/models/FileMongo.js b/models/FileMongo.js index 28d83f7..5f7d091 100644 --- a/models/FileMongo.js +++ b/models/FileMongo.js @@ -14,7 +14,7 @@ var FileMongoShema = new Schema({ type: String, unique: false }, - extention: { + type: { type: String, unique: false }, diff --git a/package-lock.json b/package-lock.json index c4ac6c1..bea9b22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11550,6 +11550,11 @@ "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" }, + "oauth": { + "version": "0.9.15", + "resolved": "https://registry.npmjs.org/oauth/-/oauth-0.9.15.tgz", + "integrity": "sha1-vR/vr2hslrdUda7VGWQS/2DPucE=" + }, "oauth-sign": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.8.2.tgz", @@ -12109,6 +12114,47 @@ "pause": "0.0.1" } }, + "passport-facebook": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/passport-facebook/-/passport-facebook-2.1.1.tgz", + "integrity": "sha1-w50LUq5NWRYyRaTiGnubYyEwMxE=", + "requires": { + "passport-oauth2": "1.4.0" + } + }, + "passport-github": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/passport-github/-/passport-github-1.1.0.tgz", + "integrity": "sha1-jOHj/NYa11eOsd9ZWDnkrqEjVdQ=", + "requires": { + "passport-oauth2": "1.4.0" + } + }, + "passport-google-oauth": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-google-oauth/-/passport-google-oauth-1.0.0.tgz", + "integrity": "sha1-ZfUGMxkq0GJ6GLCJYAdxCdhOt20=", + "requires": { + "passport-google-oauth1": "1.0.0", + "passport-google-oauth20": "1.0.0" + } + }, + "passport-google-oauth1": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-google-oauth1/-/passport-google-oauth1-1.0.0.tgz", + "integrity": "sha1-r3SoA99R7GRvZqRNgigr5vEI4Mw=", + "requires": { + "passport-oauth1": "1.1.0" + } + }, + "passport-google-oauth20": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/passport-google-oauth20/-/passport-google-oauth20-1.0.0.tgz", + "integrity": "sha1-O5YOih1w0dvnlGFcgnxoxAOSpdA=", + "requires": { + "passport-oauth2": "1.4.0" + } + }, "passport-jwt": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/passport-jwt/-/passport-jwt-3.0.1.tgz", @@ -12132,6 +12178,27 @@ } } }, + "passport-oauth1": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/passport-oauth1/-/passport-oauth1-1.1.0.tgz", + "integrity": "sha1-p96YiiEfnPRoc3cTDqdN8ycwyRg=", + "requires": { + "oauth": "0.9.15", + "passport-strategy": "1.0.0", + "utils-merge": "1.0.1" + } + }, + "passport-oauth2": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/passport-oauth2/-/passport-oauth2-1.4.0.tgz", + "integrity": "sha1-9i+BWDy+EmCb585vFguTlaJ7hq0=", + "requires": { + "oauth": "0.9.15", + "passport-strategy": "1.0.0", + "uid2": "0.0.3", + "utils-merge": "1.0.1" + } + }, "passport-strategy": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz", @@ -14665,6 +14732,11 @@ "integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=", "optional": true }, + "uid2": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/uid2/-/uid2-0.0.3.tgz", + "integrity": "sha1-SDEm4Rd03y9xuLY53NeZw3YWK4I=" + }, "ultron": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.0.2.tgz", diff --git a/package.json b/package.json index 7938cd7..8cb4913 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,9 @@ "ng-html-util": "1.0.x", "ngm-cli": "0.5.x", "passport": "^0.4.0", + "passport-facebook": "^2.1.1", + "passport-github": "^1.1.0", + "passport-google-oauth": "^1.0.0", "passport-jwt": "^3.0.1", "rxjs": "^6.0.0-rc.1", "rxjs-compat": "^6.0.0-rc.1", diff --git a/routes/api.js b/routes/api.js index d65f83c..9cb3e0b 100644 --- a/routes/api.js +++ b/routes/api.js @@ -13,6 +13,14 @@ var fs = require('fs'); var multer = require('multer'); var upload = multer({ dest: './public/' }); +var passportFacebook = require('../auth/facebook'); +var passportGoogle = require('../auth/google'); +var passportGitHub = require('../auth/github'); + + + + + /* creation Token */ getToken = function (headers) { if (headers && headers.authorization) { @@ -215,4 +223,44 @@ router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), f } }); + + +//////////// + + + +/* GET users listing. */ +router.get('/', ensureAuthenticated, function(req, res, next) { + res.render('user', { user: req.user }); +}); + +function ensureAuthenticated(req, res, next) { + if (req.isAuthenticated()) { return next(); } + res.redirect('/login') +} + +/* FACEBOOK ROUTER */ +router.get('/facebook', passportFacebook.authenticate('facebook')); + +router.get('/facebook/callback', passportFacebook.authenticate('facebook', { failureRedirect: '/login' }), function(req, res) { + // Successful authentication, redirect home. + res.redirect('/'); + }); + +/* GOOGLE ROUTER */ +router.get('/google', passportGoogle.authenticate('google', { scope: 'https://www.google.com/m8/feeds' })); + +router.get('/google/callback', passportGoogle.authenticate('google', { failureRedirect: '/login' }), function(req, res) { + res.redirect('/'); + }); + +/* GITHUB ROUTER */ +router.get('/github', passportGitHub.authenticate('github', { scope: [ 'user:email' ] })); + +router.get('/github/callback', passportGitHub.authenticate('github', { failureRedirect: '/login' }), function(req, res) { + // Successful authentication, redirect home. + res.redirect('/'); + }); + + module.exports = router; diff --git a/src/app/book/book.component.html b/src/app/book/book.component.html index 82610d1..cb0841f 100644 --- a/src/app/book/book.component.html +++ b/src/app/book/book.component.html @@ -77,30 +77,32 @@ {{ file.taille / ( 1024 * 1024 ) | number : '1.2-2'}} Mo - {{ file.extention }} + {{ file.type }} {{ file.lastDate }} -
+
-
+
-
+
-
- +
+
-
+
- +
-
+
diff --git a/src/app/book/book.component.ts b/src/app/book/book.component.ts index 8c82704..6d77d79 100644 --- a/src/app/book/book.component.ts +++ b/src/app/book/book.component.ts @@ -8,6 +8,7 @@ import {Http} from "@angular/http"; import {Form} from "@angular/forms"; import * as formData from 'form-data'; import {forEach} from '@angular/router/src/utils/collection'; +import {type} from 'os'; @Component({ selector: 'app-book', @@ -25,12 +26,13 @@ export class BookComponent implements OnInit, OnDestroy { httpOptions: any; isClickCreateFolder: boolean; inputCreateFolder: String; - + reader: FileReader; + test: String; constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute) { this.isClickCreateFolder = false; this.fileChooseName = 'None'; this.filesToUpload = []; - + this.test = ''; } ngOnInit() { @@ -80,24 +82,50 @@ export class BookComponent implements OnInit, OnDestroy { //var preview = document.querySelector('img'); + var meee = this; this.http.post('/api/getFileList', {mainPath : this.mainFolder.path, owner: this.currentUser.username.toString()}).subscribe(files => { if (files) { + // this.allUserFile = files; + this.allUserFile = files; - /* - for (let f of this.allUserFile) { - console.log('f.extention : ' + f.extention); - if (f.extention === ('text/plain')) { - this.reader = new FileReader(); - let contenido; - console.log('ffff : ' + JSON.stringify(f)); - this.reader.onload = (e) => { - contenido = this.reader.result; - console.log('contenido : ' + contenido); - }; - this.reader.readAsText(f); - } - } */ + + + // for (let f of this.allUserFile) { + // + // console.log('f.type : ' + f.type); + // + // + // /* + // var textFileAsBlob = new Blob([f.blob], { + // type: 'text/plain' + // });*/ + // + // if (f.type === ('text/plain')) { + // JSON.stringify(f); + // //f.function( fii => { + // this.reader = new FileReader(); + // var ff = new Blob([JSON.stringify(f, null, 2)], {type: 'text/plain'}) + // var me = this; + // this.reader.onload = function(e:any) { + // me.test = e.target.result; + // console.log('meee.test : ' + JSON.stringify(e.target.result)); + // }; + // //ff.type = f.type; + // //ff.size = f.taille; + // //ff.slice(f); + // var gugu = new File(('../../../public/' + f._id)[], f.name); + // this.reader.readAsText('../../../public/' + f._id.toString(), f.name); + // + // //console.log('couilles : ' + meee.reader.readAsText(fii)); + // + // //console.log('couilles : ' + JSON.stringify(f)); + // //}); + // + // + // } + // } + } else { this.allUserFile = []; } @@ -164,7 +192,7 @@ export class BookComponent implements OnInit, OnDestroy { upload() { if(this.mainFolder !== undefined){ - this.http.post('api/uploadFileMongo', { name: this.fileChooseName.toString(), path: this.mainFolder.path.toString(), extention: this.filesToUpload[0].type.toString(), taille: this.filesToUpload[0].size.toString(), idUser: this.currentUser._id.toString(), owner: this.currentUser.username.toString(), lastDate: Date.now().toString() }).subscribe(resp => { + this.http.post('api/uploadFileMongo', { name: this.fileChooseName.toString(), path: this.mainFolder.path.toString(), type: this.filesToUpload[0].type.toString(), taille: this.filesToUpload[0].size.toString(), idUser: this.currentUser._id.toString(), owner: this.currentUser.username.toString(), lastDate: Date.now().toString(), data: ''}).subscribe(resp => { this.makeFileRequest('http://localhost:3000/api/upload', [], this.filesToUpload, resp).then((result) => { this.getfiles(); this.filesToUpload = null; diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 9e9068e..c1d94f5 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -15,3 +15,9 @@

+ + + + + + \ No newline at end of file