From 6a7f64c044177afc32c9c1c53af69a01d46cba92 Mon Sep 17 00:00:00 2001 From: Tmadkaud Date: Fri, 8 Jun 2018 04:57:29 +0200 Subject: [PATCH] version stable 08 06 2018 --- auth/facebook.js | 70 +++++++++++++++++++++++----- bin/www | 2 +- models/User.js | 4 ++ routes/api.js | 52 ++++++++++++--------- src/app/book/book.component.html | 16 +++---- src/app/book/book.component.ts | 78 +++++++++++++++----------------- src/app/home/home.component.html | 3 +- src/app/home/home.component.ts | 5 +- src/app/main/main.component.html | 4 +- src/app/main/main.component.ts | 37 +++++++-------- 10 files changed, 158 insertions(+), 113 deletions(-) diff --git a/auth/facebook.js b/auth/facebook.js index 89b3ef3..6263984 100644 --- a/auth/facebook.js +++ b/auth/facebook.js @@ -1,27 +1,73 @@ var passport = require('passport'); var FacebookStrategy = require('passport-facebook').Strategy; var User = require('../models/user'); -// module.exports = function(passport){ -// -// }; +var jwt = require('jsonwebtoken'); +var config = require('../config/database'); + var mongoose = require('mongoose'); + + +passport.serializeUser((user, done) => { + done(null, user.id); +}); + +passport.deserializeUser((id, done) => { + // console.log(`id: ${id}`); + User.findById(id) + .then(user => { + done(null, user); + }) + .catch(error => { + console.log(`Error: ${error}`); + }); +}); + passport.use(new FacebookStrategy({ clientID: "191092458179642", clientSecret: "964e490d7e49a13ee7a77c9b65419fb3", - callbackURL: "https://localhost:3000/api/facebook/callback" + callbackURL: "https://localhost:3000/api/facebook/callback", + proxy: true }, function(accessToken, refreshToken, profile, done) { console.log('facebook findOneOrCreate'+profile.displayName); - User.findOneOrCreate(profile.displayName.toString(), function(err, user, req, res) { - if (err) { - console.log('findOneOrCreate err'); - console.log(err); - return done(err); + + User.findOne({ + username: profile.displayName + }, function(err, user) { + if(user){ + console.log('findOneOrCreate ok'+user.username); + var token = jwt.sign(user.toJSON(), config.secret); + // return the information including token as JSON + //res.json({success: true, token: }); + return done(null, user); + }else{ + + var newUser = new User({ + username: profile.displayName, + password: profile.password, + social: true + }); + // save the user + newUser.save(function(err) { + if (err) { + console.log('Username already exists.'); + } + return done(null, user); + }); } - console.log('findOneOrCreate ok'+user.displayName); - //res.json({token: accessToken, user: user.username}); - done(null, user); + }); + + // User.findOneOrCreate(profile.displayName.toString(), function(err, user, req, res) { + // if (err) { + // console.log('findOneOrCreate err'); + // console.log(err); + // return done(err); + // } + // console.log('findOneOrCreate ok'+user.displayName); + // //res.json({token: accessToken, user: user.username}); + // done(null, user); + // }); // Page.findOneOrCreate(pageId, (err, page)=>{ // if(err){ // //if theres an error, do something diff --git a/bin/www b/bin/www index 9d5403d..dc89cc7 100644 --- a/bin/www +++ b/bin/www @@ -37,7 +37,7 @@ var credentials = { var server = https.createServer(credentials, app); //var server = http.createServer(app); -W + // server.setSecure(credentials); /** diff --git a/models/User.js b/models/User.js index dc1c094..ad19eaf 100644 --- a/models/User.js +++ b/models/User.js @@ -15,6 +15,10 @@ var UserSchema = new Schema({ password: { type: String, required: false + }, + social: { + type: Boolean, + required: false } }); diff --git a/routes/api.js b/routes/api.js index 83a76af..2d799e7 100644 --- a/routes/api.js +++ b/routes/api.js @@ -38,15 +38,7 @@ getToken = function (headers) { } }; -/* config multer dossier cible et nom du file : enregistrement du fichier dans le dossier public*/ -const storage = multer.diskStorage({ - destination: function (req, file, cb) { - cb(null, './public/') - }, - filename: function (req, file, cb) { - cb(null, file.originalname) - } -}); + router.use(function(req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); @@ -120,12 +112,10 @@ router.post('/deleteFileMongo', function(req, res, next) { console.log(JSON.stringify(req.body)); FileMongo.remove({_id : req.body._id, name: req.body.name, owner: req.body.owner}, function (err, post) { if (err){ - console.log('err deleteF ileMongo : ' + err); return next(err); } //Delete file multer dans Public - //console.log('this.getStringExtention(req.body) : ' + getStringExtention(req.body)); fs.unlink('./public/' + req.body._id + getStringExtention(req.body)); res.json(post); @@ -251,7 +241,14 @@ router.post('/getFileList', function(req, res) { }); }); +router.post('/getUserById', function(req, res) { + User.findOne({ + _id: req.body.id + }, function (err, user) { + res.json(user); + }); +}); /* Login */ router.post('/signin', function(req, res) { User.findOne({ @@ -278,6 +275,15 @@ router.post('/signin', function(req, res) { }); }); +/* config multer dossier cible et nom du file : enregistrement du fichier dans le dossier public*/ +const storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, './public/') + }, + filename: function (req, file, cb) { + cb(null, file.originalname) + } +}); /* Multer upload */ router.post('/upload' , multer({storage: storage, limits: {fileSize: 30000000000}}).array("public[]", 12) ,function(req,res,next){ @@ -285,7 +291,7 @@ router.post('/upload' , multer({storage: storage, limits: {fileSize: 30000000000 }); /* Get utilisateur courant */ -router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), function(req, res) { +router.get('/getCurrentUser', passport.authenticate('jwt'), function(req, res) { var token = getToken(req.headers); if (token) { @@ -309,21 +315,23 @@ function ensureAuthenticated(req, res, next) { res.redirect('/api/login'); } /* FACEBOOK ROUTER */ -router.get('/facebook', passportFacebook.authenticate('facebook', { session: false}), +router.get('/facebook', passportFacebook.authenticate('facebook'), function(req, res) { - console.log('fffacebook' + JSON.stringify(tess)); + console.log('facebook user : '+ req.user ); // Successful authentication, redirect home. res.json({user: req.user}); - }); +}); -router.get('/facebook/callback', - passportFacebook.authenticate('facebook', { failureRedirect: '/' , session: false}), - function(req, res, user,accessToken, refreshToken, profile, done) { - var tess = {user:user,accessToken:accessToken, refreshToken:refreshToken, profile:profile, done:done}; - console.log('fffacebook' + JSON.stringify(tess)); +router.get('/facebook/callback', passportFacebook.authenticate('facebook'), + function(req, res) { + console.log('facebook user : '+ req.user ); + JSON.stringify(req.user); + var myUser = req.user.toString(); + //res.json({user: myUser}); + res.redirect('/main/'+req.user._id); // Successful authentication, redirect home. - res.redirect(['/main/1']); - }); + +}); /* GOOGLE ROUTER */ diff --git a/src/app/book/book.component.html b/src/app/book/book.component.html index 0b21cd2..d400cdd 100644 --- a/src/app/book/book.component.html +++ b/src/app/book/book.component.html @@ -4,13 +4,13 @@
-

Current Folder : {{mainFolder.name}}

+

Current Folder : {{mainFolder?.name}}

Used space : {{sizeFile.toString() === '0' ? '0' : (sizeFile/ ( 1024 * 1024 ) | number : '1.2-2')}} / 30 Mo

-

Path : {{mainFolder.path}}

+

Path : {{mainFolder?.path}}

@@ -26,7 +26,7 @@
- + @@ -115,11 +115,11 @@
diff --git a/src/app/book/book.component.ts b/src/app/book/book.component.ts index 76299ac..1ef596e 100644 --- a/src/app/book/book.component.ts +++ b/src/app/book/book.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, OnDestroy, ViewChild, Output, EventEmitter } from '@angular/core'; +import { Component, OnInit, OnDestroy, ViewChild, Output, Input, EventEmitter } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Router, ActivatedRoute } from '@angular/router'; import { Observable } from 'rxjs/Observable'; @@ -18,12 +18,15 @@ import { DropzoneComponent , DropzoneDirective, import { CompleterService } from '../typescripts/pro'; + + @Component({ selector: 'app-book', templateUrl: './book.component.html', styleUrls: ['./book.component.css'] }) export class BookComponent implements OnInit, OnDestroy { + @Input() socialIdUser: String; @Output() setAllUserAppFolder = new EventEmitter(); message: string = 'Hola Mundo!'; @@ -32,6 +35,7 @@ export class BookComponent implements OnInit, OnDestroy { currentUser: any; allUserFile: any; allUserFolder: any; + //socialIdUser: String; filterAllUserFolder: any; allUserAppFolder: any; allUserAppFile: any; @@ -55,18 +59,6 @@ export class BookComponent implements OnInit, OnDestroy { searchStr: String; dataService: any; - protected searchData = [ - { color: 'red'}, - { color: 'green'}, - { color: 'blue'}, - { color: 'cyan'}, - { color: 'magenta'}, - { color: 'yellow'}, - { color: 'black'}, - ]; - - - public type: string = 'component'; public disabled: boolean = false; @@ -84,19 +76,9 @@ export class BookComponent implements OnInit, OnDestroy { constructor(private completerService: CompleterService, private http: HttpClient, private router: Router, private route: ActivatedRoute, public sanitizer: DomSanitizer) { - this.httpOptions = { - headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken'), 'Access-Control-Allow-Origin' : '*'}), - pathFolder: '' - }; - - this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => { - this.currentUser = user; - this.openFolder('Home'); - }, err => { - if (err.status === 401) { - this.router.navigate(['Home']); - } - }); + + + this.isClickCreateFolder = false; this.fileChooseName = 'None'; this.filesToUpload = []; @@ -105,7 +87,6 @@ export class BookComponent implements OnInit, OnDestroy { } sendMessage() { - console.log('caca'); this.messageEvent.emit('caca') } public toggleType(): void { @@ -150,13 +131,12 @@ export class BookComponent implements OnInit, OnDestroy { this.filesToUpload = args; this.fileChooseName = args[0].name; - var reader = new FileReader(); - reader.onload = function() { - alert(reader.result); - }; - reader.readAsText(args[0]); + // var reader = new FileReader(); + // reader.onload = function() { + // alert(reader.result); + // }; + // reader.readAsText(args[0]); - console.log('ggguu :' + JSON.stringify(args)); this.upload(); this.resetDropzoneUploads(); this.openFolder(this.mainFolder.path); @@ -164,12 +144,32 @@ export class BookComponent implements OnInit, OnDestroy { } ngOnInit() { - + this.httpOptions = { + headers: new HttpHeaders({ + 'Authorization': localStorage.getItem('jwtToken'), + 'Access-Control-Allow-Origin' : '*', + 'Access-Control-Allow-Methods' : 'GET, POST, OPTIONS, PUT, PATCH, DELETE', + 'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept' + }), + pathFolder: '' + }; + // this.httpOptions = { + // headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken'), 'Access-Control-Allow-Origin' : '*'}), + // pathFolder: '' + // }; + + this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => { + this.currentUser = user; + this.openFolder('Home'); + }, err => { + if (err.status === 401) { + this.router.navigate(['login']); + } + }); } deleteFile(fileToDelete) { this.http.post('/api/deleteFileMongo', fileToDelete).subscribe(file => { - console.log('this.mainFolder.path : ' + this.mainFolder.path); this.openFolder(this.mainFolder.path); }); } @@ -203,7 +203,6 @@ export class BookComponent implements OnInit, OnDestroy { } getAllUserAppFiles() { - console.log(this.currentUser.username.toString()); this.http.post('/api/getFileAppList', {owner: this.currentUser.username.toString()}).subscribe(files => { if (files) { this.allUserAppFile = files; @@ -213,7 +212,7 @@ export class BookComponent implements OnInit, OnDestroy { this.dataService = this.completerService.local(this.allItems, 'name', 'path'); - for (let f of this.allUserAppFile){ + for (let f of this.allItems){ console.log(f.name); //f.fileTab = this.getfiles(f); } @@ -239,7 +238,6 @@ export class BookComponent implements OnInit, OnDestroy { for (let f of this.allUserFile) { this.sizeFile += +f.taille; - console.log('this.sizeFile : ' + this.sizeFile); if(f.type === ('image/jpeg') || f.type === ('image/png')){ f.data = f.url; f.urlSafe = this.sanitizer.bypassSecurityTrustUrl(f.url); @@ -270,7 +268,6 @@ export class BookComponent implements OnInit, OnDestroy { this.http.post('/api/getFolderAppList', {owner: this.currentUser.username.toString()}).subscribe(folders => { if (folders) { this.allUserAppFolder = folders; - console.log(this.currentUser.username.toString()); this.setAllUserAppFolder.emit(this.currentUser.username.toString()); @@ -320,13 +317,11 @@ export class BookComponent implements OnInit, OnDestroy { } this.httpOptions.pathFolder = path; - this.isClickCreateFolder = false; this.http.post('/api/getMainFolder', {path: path, owner: this.currentUser.username.toString()}).subscribe(folder => { if (folder) { this.mainFolder = folder; - console.log('folder : ' + folder); this.getfolders(); this.getAllUserAppFolder(); this.getfiles(); @@ -368,7 +363,6 @@ export class BookComponent implements OnInit, OnDestroy { upload() { var me = this; if(this.mainFolder !== undefined){ - console.log('this.filesToUpload[0] : ' + JSON.stringify(this.filesToUpload[0])); 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(), url: ''}).subscribe(resp => { me.saveURLFileMongo(resp); var mee = this; diff --git a/src/app/home/home.component.html b/src/app/home/home.component.html index 0307f1b..6255d5d 100644 --- a/src/app/home/home.component.html +++ b/src/app/home/home.component.html @@ -348,4 +348,5 @@ - \ No newline at end of file + +Login with face \ No newline at end of file diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index c8ed439..397cde6 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -37,8 +37,6 @@ export class HomeComponent implements OnInit { signup() { this.http.post('/api/signup',this.signupData).subscribe(resp => { - // console.log(resp); - //this.router.navigate(['home']); this.showregister(); }, err => { this.messageR = err.error.msg; @@ -46,9 +44,8 @@ export class HomeComponent implements OnInit { } facebook() { - console.log('tete') + console.log('teteeeeeeee'); this.http.get('/api/facebook').subscribe(resp => { - console.log('tete'); this.router.navigate(['main']); }, err => { this.message = err.error.msg; diff --git a/src/app/main/main.component.html b/src/app/main/main.component.html index e6e7c81..a18fa30 100644 --- a/src/app/main/main.component.html +++ b/src/app/main/main.component.html @@ -53,7 +53,7 @@ @@ -141,7 +141,7 @@
- +
diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index f79d7f4..f9ebe1a 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -13,7 +13,7 @@ export class MainComponent implements OnInit { allUserAppFile: any; mainFolder: any; currentUser: any; - + socialIdUser: String; constructor(private router: Router, private route: ActivatedRoute, private http: HttpClient) { } logout() { @@ -22,12 +22,17 @@ export class MainComponent implements OnInit { } ngOnInit() { - this.httpOptions = { - headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken'), 'Access-Control-Allow-Origin' : '*'}), + headers: new HttpHeaders({ + 'Authorization': localStorage.getItem('jwtToken'), + 'Access-Control-Allow-Origin' : '*', + 'Access-Control-Allow-Methods' : 'GET, POST, OPTIONS, PUT, PATCH, DELETE', + 'Access-Control-Allow-Headers' : 'Origin, X-Requested-With, Content-Type, Accept' + }), pathFolder: '' }; + this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => { this.currentUser = user; if (user) { @@ -35,9 +40,15 @@ export class MainComponent implements OnInit { } }, err => { if (err.status === 401) { - this.router.navigate(['login']); + this.router.navigate(['/']); } }); + // this.httpOptions = { + // headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken'), 'Access-Control-Allow-Origin' : '*'}), + // pathFolder: '' + // }; + + } @@ -51,21 +62,10 @@ export class MainComponent implements OnInit { } setOriginFolder(path) { - //console.log( ' path : ' + path); - //var origin = []; - - //for (let f of this.allUserAppFolder){ var pathChunk = path.split('/'); var origin = pathChunk.length === 1 ? pathChunk[0] : pathChunk[pathChunk.length - 2] - //console.log( ' origin : ' + origin); - // for (var i = 0; i < pathChunk.length; i++) { - // console.log(i + ' pathChunk[i] : ' + pathChunk[i]); - // var temp = (pathChunk.length === 1 ? pathChunk[0] : pathChunk[pathChunk.length - 2]); - // console.log(i + ' temp : ' + temp); - // } - - //} + return origin; } @@ -81,14 +81,9 @@ export class MainComponent implements OnInit { } getAllUserAppFiles() { - console.log(this.currentUser.username.toString()); this.http.post('/api/getFileAppList', {owner: this.currentUser.username.toString()}).subscribe(files => { if (files) { this.allUserAppFile = files; - for (let f of this.allUserAppFile){ - //console.log(f.name); - //f.fileTab = this.getfiles(f); - } // return files; } else { this.allUserAppFile = [];