我有一个带有用户名和密码的登录表单。我正在尝试使用此处的 Nest Js 身份验证策略来验证这些凭据。因此,在相应的 auth.service.ts 文件中,我使用“nativescript core modules http”向 OAuth URL 发出 POST 请求以验证凭据。但这不起作用:
import { Injectable } from '@nestjs/common';
import { request } from "tns-core-modules/http";
const OAUTH_URL = 'url';
@Injectable()
export class AuthService {
async validateUser(username: string, password: string): Promise<any> {
let data = new FormData();
data.set('client_id', 'sb-nestjs-app!t36258');
data.set('client_secret', 'XrHuBRhyvuVNYNJNHlWLgcuBIyc=');
data.set('username', username);
data.set('password', password);
data.set('grant_type', 'password');
data.set('response_type', 'token');
request({
url: OAUTH_URL,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json;charset=utf8"
},
content: data
}).then((response) => {
console.log('response => ' + response + ' statuscode ' + response.statusCode);
if (response.statusCode == 200) {
const token = response.content['access_token'];
//TODO:
// need to send scope also
return token;
}
}, (e) => {
console.log('error' + e);
return null;
});
return null;
}
}
当我在上述代码到位后运行'nest start'时,我收到错误:找不到模块'./http-request'
我不确定这里发生了什么,我试过“npm install http-request”它也没有用。基本上,我需要将凭据发布到 NestJs 中的 OAuth url。有什么指导吗?谢谢你。