Skip to main content

Sample passport Express dùng strategy Github

Link tut: https://gist.github.com/jokecamp/65604d50227b8ea8e0d3

Đầu tiên càn tạo Github apps:
Vô Setting (profile j đó) > Github Apps (tab bên trái dưới) > new app. điền đúng thông tin redirect URL, callback (ai làm Facebook OAuth, Google 0Auth thì rõ).

Code demo:
package.json:


{
  "name": "securehelloworld",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.13.3",
    "express-session": "^1.11.3",
    "passport": "^0.3.0",
    "passport-github": "^1.0.0"
  }
}

  1 var express = require('express');
  2 var app = express();
  3 var passport = require('passport');
  4 var GithubStrategy = require('passport-github').Strategy;
  5 
  6 passport.use(new GithubStrategy({
  7     clientID: "Iv1.fac1ddc6afb44e13",
  8     clientSecret: "b2bcc3c00dcc672cf377baa08ff53f6be",
  9     callbackURL: "http://localhost:30000/auth/github/callback"
 10   },
 11   function(accessToken, refreshToken, profile, done) {
 12     // placeholder for translating profile into your own custom user object.
 13     // for now we will just use the profile object returned by GitHub
 14     return done(null, profile);
 15   }
 16 ));
 17 
 18 // Express and Passport Session
 19 var session = require('express-session');
 20 app.use(session({secret: "davzk#1234"}));
 21 app.use(passport.initialize());
 22 app.use(passport.session());
 23 
 24 passport.serializeUser(function(user, done) {
 25   // placeholder for custom user serialization
 26   // null is for errors
 27   done(null, user);
 28 });
 29 
 30 passport.deserializeUser(function(user, done) {
 31   // placeholder for custom user deserialization.
 32   // maybe you are getoing to get the user from mongo by id?
 33   // null is for errors
 34   done(null, user);
 35 });
 36 
 37 // we will call this to start the GitHub Login process
 38 app.get('/auth/github', passport.authenticate('github'));
 39 
 40 // GitHub will call this URL
 41 app.get('/auth/github/callback',
 42   passport.authenticate('github', { failureRedirect: '/' }),
 43   function(req, res) {
 44     res.redirect('/');
 45   });
 46 
 47 app.get('/', function (req, res) {
    var html = "
";
 52 
 53   // dump the user for debugging
 54   if (req.isAuthenticated()) {
 55     html += "authenticated as user:

"
 56     html += "
" + JSON.stringify(req.user, null, 4) + "
"; 57 } 58 59 res.send(html); 60 }); 61 62 app.get('/logout', function(req, res){ 63 console.log('logging out'); 64 req.logout(); 65 res.redirect('/'); 66 }); 67 68 // Simple route middleware to ensure user is authenticated. 69 // Use this route middleware on any resource that needs to be protected. If 70 // the request is authenticated (typically via a persistent login session), 71 // the request will proceed. Otherwise, the user will be redirected to the 72 // login page. 73 function ensureAuthenticated(req, res, next) { 74 if (req.isAuthenticated()) { return next(); } 75 res.redirect('/') 76 } 77 78 app.get('/protected', ensureAuthenticated, function(req, res) { 79 res.send("acess granted"); 80 }); 81 82 83 84 var server = app.listen(30000, function () { 85 //console.log('Example app listening at http://localhost:3000', 86 console.log('Example app listening at http://%s:%s', 87 //server.address().address, server.address().port); 88 "localhost", 3000); 89 });



Comments

Popular posts from this blog

AWS Elasticache Memcached connection

https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/accessing-elasticache.html#access-from-outside-aws http://hourlyapps.blogspot.com/2010/06/examples-of-memcached-commands.html Access memcached https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/GettingStarted.AuthorizeAccess.html Zip include hidden file https://stackoverflow.com/questions/12493206/zip-including-hidden-files phpmemcachedadmin ~ phpMyAdmin or phpPgAdmin ... telnet mycachecluster.eaogs8.0001.usw2.cache.amazonaws.com 11211 stats items stats cachedump 27 100 https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/VPCs.EC.html https://lzone.de/cheat-sheet/memcached VPC ID Security Group ID (sg-...) Cluster: The identifier for the cluster memcached1 Creation Time: The time (UTC) when the cluster was created January 9, 2019 at 11:47:16 AM UTC+7 Configuration Endpoint: The configuration endpoint of the cluster memcached1.ahgofe.cfg.usw1.cache.amazonaws.com:11211 St...

Notes Windows 10 Virtualbox config, PHP Storm Japanese, custom PHP, Apache build, Postgresql

 cmd => Ctrl + Shift + Enter mklink "C:\Users\HauNT\Videos\host3" "C:\Windows\System32\drivers\etc\hosts" https://www.quora.com/How-to-create-a-router-in-php https://serverfault.com/questions/225155/virtualbox-how-to-set-up-networking-so-both-host-and-guest-can-access-internet 1 NAT + 1 host only config https://unix.stackexchange.com/questions/115464/how-to-properly-set-up-2-network-interfaces-in-centos-running-in-virtualbox DEVICE=eth0 TYPE=Ethernet #BOOTPROTO=dhcp BOOTPROTO=none #IPADDR=10.9.11.246 #PREFIX=24 #GATEWAY=10.9.11.1 #IPV4_FAILURE_FATAL=yes #HWADDR=08:00:27:CC:AC:AC ONBOOT=yes NAME="System eth0" [root@localhost www]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 # Advanced Micro Devices, Inc. [AMD] 79c970 [PCnet32 LANCE] DEVICE=eth1 IPADDR=192.168.56.28 <= no eff => auto like DHCP #GATEWAY=192.168.56.1 #BOOTPROTO=dhcp BOOTPROTO=static <= no eff ONBOOT=yes HWADDR=08:00:27:b4:20:10 [root@localhost www]# ...

Rocket.Chat DB schema

_raix_push_notifications avatars.chunks avatars.files instances meteor_accounts_loginServiceConfiguration meteor_oauth_pendingCredentials meteor_oauth_pendingRequestTokens migrations rocketchat__trash rocketchat_cron_history rocketchat_custom_emoji rocketchat_custom_sounds rocketchat_import rocketchat_integration_history rocketchat_integrations rocketchat_livechat_custom_field rocketchat_livechat_department rocketchat_livechat_department_agents rocketchat_livechat_external_message rocketchat_livechat_inquiry rocketchat_livechat_office_hour rocketchat_livechat_page_visited rocketchat_livechat_trigger rocketchat_message rocketchat_oauth_apps rocketchat_oembed_cache rocketchat_permissions rocketchat_raw_imports rocketchat_reports rocketchat_roles rocketchat_room rocketchat_settings rocketchat_smarsh_history rocketchat_statistics rocketchat_subscription rocketchat_uploads system.indexes users usersSessions https://rocket.chat/docs/developer-guides/sc...