我试图在我的项目中获得一个基于json_api的登录系统。我的问题是,当我点击我的登录按钮,我得到一个未经授权的错误消息,有丢失的凭据。当我在响应中转储用户时,它显示它是空的。
这是我的控制器功能:
#[Route('/entry/login', name: 'api_login')]
public function login(#[CurrentUser] ?User $user): Response
{
if (null === $user) {
return $this->json([
'message' => 'missing credentials',
'user' => $user
], Response::HTTP_UNAUTHORIZED);
}
return $this->json(['user' => $user->getUserIdentifier()]);
}
我security.yaml:
security:
enable_authenticator_manager: true
password_hashers:
SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface: 'auto'
providers:
users:
entity:
class: 'AppEntityUser'
property: 'email'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
json_login:
check_path: api_login
和我的前端post请求:
private async submit(): Promise<void> {
if ((this as any).$refs.form.validate() && this.valid) {
const user = {
username: this.email,
password: this.password,
}
const response = await axios.post('/entry/login', user);
}
}
我一直在引用这个页面,我认为我的代码应该这样工作,但似乎我错过了一些东西。
如有任何帮助,不胜感激。
如果你需要更多的代码或任何东西,你可以问。
请尝试显式设置以下字段(username_path &password_path):
firewall:
main:
lazy: true
json_login:
check_path: api_login
username_path: email
password_path: password
因为您正在使用实体property: 'email'
。同时在你的前端请求中将username
改为email
。