te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Get current logged in username using Passport JS? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get current logged in username using Passport JS? - Stack Overflow

programmeradmin4浏览0评论

I have created a simple user login application following an online tutorial using Node, Express and Passport. Login works correctly but I would like to get the username of the current logged in user and I can't seem to get this working.

I have the following in my app.js:

/// Configuring Passport
var passport = require('passport');
var expressSession = require('express-session');

app.use(expressSession({
  secret: 'cookie_secret',
    name: 'cookie_name',
    proxy: true,
    resave: true,
    saveUninitialized: true
}));

app.use(passport.initialize());
app.use(passport.session({
    secret: 'cookie_secret',
    name: 'cookie_name',
    proxy: true,
    resave: true,
    saveUninitialized: true
}));

From what I read in similar posts, I need to expose the username so that I may use it in other files. I tried to do this in my app.js:

app.get('/home', function(req, res) {
  res.render('home.jade', { username: req.user.username });
});

Home is a route where I would like to use the the username, and I am trying to access the username with an alert like the following:

alert(req.user.username);

This does not work, the req object is undefined...I'm not sure what I am missing?


Managed to get this working. In my app.js I have:

app.get('/home', function(req, res) {
  res.render('home.jade', { username: req.user.username });
});

And then in my /home route I can access the username by doing:

req.user.username

I have created a simple user login application following an online tutorial using Node, Express and Passport. Login works correctly but I would like to get the username of the current logged in user and I can't seem to get this working.

I have the following in my app.js:

/// Configuring Passport
var passport = require('passport');
var expressSession = require('express-session');

app.use(expressSession({
  secret: 'cookie_secret',
    name: 'cookie_name',
    proxy: true,
    resave: true,
    saveUninitialized: true
}));

app.use(passport.initialize());
app.use(passport.session({
    secret: 'cookie_secret',
    name: 'cookie_name',
    proxy: true,
    resave: true,
    saveUninitialized: true
}));

From what I read in similar posts, I need to expose the username so that I may use it in other files. I tried to do this in my app.js:

app.get('/home', function(req, res) {
  res.render('home.jade', { username: req.user.username });
});

Home is a route where I would like to use the the username, and I am trying to access the username with an alert like the following:

alert(req.user.username);

This does not work, the req object is undefined...I'm not sure what I am missing?


Managed to get this working. In my app.js I have:

app.get('/home', function(req, res) {
  res.render('home.jade', { username: req.user.username });
});

And then in my /home route I can access the username by doing:

req.user.username
Share Improve this question edited Mar 22, 2016 at 0:43 user2573690 asked Mar 21, 2016 at 1:28 user2573690user2573690 6,0139 gold badges45 silver badges64 bronze badges 4
  • You're accesing it incorrectly in Jade, it won't be req.user.username but just username, so do something like div #{username} in your view, and see if it outputs. – adeneo Commented Mar 21, 2016 at 1:34
  • That works when accessing the username in Jade, but how can I access the username in a different javascript file in my project? For example, to use the username and filter records that I get from a collection in a route js file. – user2573690 Commented Mar 21, 2016 at 1:39
  • The javascript file isn't parsed by Jade, so you can't. You would have to output the username in the HTML created by Jade, either as a data attribute or in it's own script tag, and then get the data from there in the other javascript file – adeneo Commented Mar 21, 2016 at 1:44
  • Oh I see, i thought there was a way to grab the username in js. I'm trying what you said, so I wrapped the username in a div like this | <div id='userid'>#{user.username}</div> and then I am trying to access it in my js file using alert(document.getElementById("userid").value); but I keep getting undefined, am I using the div correctly in the jade file? – user2573690 Commented Mar 21, 2016 at 2:19
Add a ment  | 

3 Answers 3

Reset to default 8

You are mixing two things, one is the client side, and the other is the server side, both use javascript but for render server side code in the cliente side you could not use directly in the client side. you must pass to the view as you do with

app.get('/home', function(req, res) {
    res.render('home.jade', { username: req.user.username });
});

here you expose the username variable to the view

In the jade file you should do this

alert(#{username})

instead of

alert(req.user.username)

try:

app.use(cookieParser());
app.use(session({
            secret: 'cookie_secret',
            name: 'cookie_name',
            proxy: true,
            resave: true,
            saveUninitialized: true
        })
    );

app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

on controller use req.user

{ username: user.local.username }

Please go with the below link

https://github./hnagarkoti/passportauthenticationWorking

发布评论

评论列表(0)

  1. 暂无评论