gpt4 book ai didi

javascript - 用于 Rocket Chat iframe 身份验证的 NodeJS 应用程序抛出 SyntaxError : Unexpected token (

转载 作者:行者123 更新时间:2023-12-03 01:06:33 25 4
gpt4 key购买 nike

我有一个 NodeJS 应用程序,每次我尝试使用命令“node index.js”启动该应用程序时,它都会抛出错误:

app.post('/login', async (req, res) => {
^

SyntaxError: Unexpected token (

我了解这个错误的普遍性,但我需要一些指导。该应用程序专门用于 Rocket.chats 文档中详细介绍的 iframe-auth,用于利用第 3 方 API 来登录,而不是 Rocket Chat 的 native 登录系统。

应用程序的完整代码在这里:

var express = require('express');
var bodyParser = require('body-parser');
var request = require('request-promise');
var session = require('express-session');
const cookieParser = require('cookie-parser');
var app = express();
const rocketChatServer = '*redacted*';
const rocketChatAdminUserId = '*redacted*';
const rocketChatAdminAuthToken = '*redacted*';

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

//CORS in case you need
app.use((req, res, next) => {
res.set('Access-Control-Allow-Origin', '*redacted*');
res.set('Access-Control-Allow-Credentials', 'true');

next();
});

// define middleware
const middleware = [

cookieParser(),
session({
secret: 'super-secret-key',
key: 'super-secret-cookie',
resave: true,
saveUninitialized: true,
cookie: { maxAge: 1209600000 } // two weeks in miliseconds
})
]
app.use(middleware);
//var createOrLoginUser = require('./rocketchat');

app.get('/', (req, res) => {

if(req.session.rocketchatAuthToken){

res.send(`
<html>
<body>
<iframe width='100%;' height='100%' src="*redacted*"
frameborder="0"></iframe>
</body>
</html>`);

}else{

res.send(`
<form method="post" action="/login">
<input type="text" placeholder="username" name="username">
<input type="text" placeholder="firstName"
name="firstname">
<input type="text" placeholder="Email" name="email">
<input type="text" placeholder="Pass" name="password">
<input type="submit" value="Submit">
</form>

`);
}

})
app.post('/login', async (req, res) => {
// ....CODE TO LOGIN USER

var username = req.body.username;
var pass = req.body.password;
var email = req.body.email;
var name = req.body.firstname;

// Creating or login user into Rocket chat
try {

// Request of login for 3rd party API

var options = { method: 'GET',
url: '*redacted*',
qs:
{ hashKey: '*redacted*',
sitename: username,
password: pass },
headers:
{ 'Postman-Token': '*redacted*',
'Cache-Control': 'no-cache' } };

const third_party_response = await request(options);
var result_third_party = JSON.parse(third_party_response);
console.log(result_third_party);
console.log(result_third_party.email);

if(result_third_party.email != ''){

// Request of login for Rocket Chat

var options = { method: 'POST',
url: `${rocketChatServer}/api/v1/login`,
headers:
{ 'Postman-Token': '*redacted*',
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded' },
form: { username: username, password: pass } };

const response = await request(options);
var result = JSON.parse(response);

req.session.rocketchatAuthToken = result.data.authToken;
res.redirect('/');








}else{
res.send('You are not registered with 3rd party API!');
}




} catch (ex) { console.log('CATHCh');
if (ex.statusCode === 401) {

// User does not exist, creating user
var options = { method: 'POST',
url: '*redacted*',
headers:
{ 'Postman-Token': '*redacted*',
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded',
'X-User-Id': rocketChatAdminUserId,
'X-Auth-Token': rocketChatAdminAuthToken },
form:
{ name: name ,
email: email,
password: pass,
username: username } };

const response_register = await request(options);
var result_register = JSON.parse(response_register);

if(result_register.success){
// on success login users
var options = { method: 'POST',
url: `${rocketChatServer}/api/v1/login`,
headers:
{ 'Postman-Token': '*redacted*',
'Cache-Control': 'no-cache',
'Content-Type': 'application/x-www-form-urlencoded' },
form: { username: username, password: pass } };

const response = await request(options);
var result = JSON.parse(response);
req.session.rocketchatAuthToken = result.data.authToken;
res.redirect('/');

}else{
console.log('Failed to create account');
}
// Perfom login
//return await loginUser(email, password);
} else {
throw ex;
}
}
})


// This method will be called by Rocket.chat to fetch the login token
app.get('/api/v1/rocket_chat_auth_get', (req, res) => {

if (req.session.rocketchatAuthToken) {
res.send({ loginToken: req.session.rocketchatAuthToken })
return;
} else {
res.status(401).json({ message: 'User not logged in'});
return;
}
})

// This method will be called by Rocket.chat to fetch the login token
// and is used as a fallback
app.get('/api/v1/rocket_chat_iframe', (req, res) => {
const rocketChatServer = '*redacted*';
if (req.session.rocketchatAuthToken) {
return res.send(`<script>
window.parent.postMessage({
event: 'login-with-token',
loginToken: '${ req.session.rocketchatAuthToken }'
}, '${ rocketChatServer }');
</script>
`)
return;
} else {
return res.redirect('/login');
}
})

app.listen(3030, function () {
console.log('Example app listening on port 3030!');
});

最佳答案

您使用的 Node 版本肯定错误。 async 关键字需要 Node 8 或更高版本。

关于javascript - 用于 Rocket Chat iframe 身份验证的 NodeJS 应用程序抛出 SyntaxError : Unexpected token (,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52376989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com