I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

huangapple go评论68阅读模式
英文:

I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

问题

请帮我解决这个问题。在我的NextJs应用中,我使用Google登录,但遇到了问题。为什么?
这是我的[...nextauth]文件...
这是我的Nav组件,我想传递登录函数...
当我点击它时,它返回404响应,但我应该看到下面这个:
如何解决?

英文:

Pleas Help me To solve this problem. http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?
Here is my [...nextauth] file ...
I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

Here is my Nav component where I want to pass the signin function...

I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

When I click on it, it returns a 404 response yet I am supposed to see this below:
I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

How I Sove It?

答案1

得分: 1

Sure, here's the translated code snippet:

如果您因为用户名中有多个空格而无法登录请将以下部分更改

    await User.create({
        email: profile.email,
        username: profile.name.replace(" ", "").toLowerCase(),
        image: profile.picture
    })

更改为

    await User.create({
        email: profile.email,
        username: profile.name.replace(/\s/g, "").toLowerCase(),
        image: profile.picture
    })

Please let me know if you need any further assistance.

英文:

This comment from the gist solved mine:
https://gist.github.com/adrianhajdin/6df61c6cd5ed052dce814a765bff9032?permalink_comment_id=4572572#gistcomment-4572572

if you're having trouble signing in because your username has multiple white space
change the

await User.create({
    email: profile.email,
    username: profile.name.replace(" ", "").toLowerCase(),
    image: profile.picture
})

to

await User.create({
    email: profile.email,
    username: profile.name.replace(/\s/g, "").toLowerCase(),
    image: profile.picture
})

答案2

得分: 1

我遵循js Mastery的相同教程,可能有趣的是,我通过检查终端消息发现只是一个未声明的变量问题。简而言之,我想告诉你仔细检查你的终端或者你的编辑器的终端,一个错误会在那里显示,可能与我的不同。

在学习Next.js的开始阶段,我们不太习惯检查终端,但所有的错误都会在那里显示,所以只需检查它,它会帮助你解决这个问题。

英文:

I am following the same tutorial by js Mastery, It may be funny but I solve my problem by checking the terminal message was just consoling an undeclared variable. In short, I want to say you that double-check your terminal or your editor's terminal and an error will be showing there it can be different than mine.
At the start of learning Next.js we don't have that much habit to check the terminal but all errors are written there so just check it and it will help you to solve this problem

答案3

得分: 1

问题将在您的终端中提到

检查coonectToDB函数是否引发错误

await connectToDB();

如果您没有看到MongoDB connected,那么问题可能出在该函数中。

检查用户对象是否引发任何错误(如果已定义...)。

const userExists = await User.findOne({ 
    email: profile.email, 
});

检查您的Google帐户名称是否遵循您在models/user.js文件中设置的规则:

// models/user.js文件
username: {
    type: String,
    required: [true, 'Username is required!'],
    match: [
        /^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/,
        "Username invalid, it should contain 8-20 alphanumeric letters and be unique!"
    ] 
}

如果不是,那么您将在控制台中找到以下错误:

Error: User validation failed: username: Username invalid, it should contain 8-20 alphanumeric letters and be unique!

名称应该是:

  1. 仅包含:

    • 英文字母 A-Za-z(除此之外的任何字符都不允许)
    • 数字 0-9
    • 下划线 _
    • 点 .
  2. 字符数在8到20之间

  3. 不允许名称中出现下划线和点的任何组合,例如:

    • example_.example
    • example_.example
    • example__example
    • example..example
英文:

The problem will be mention in Your Terminal.

Check if the coonectToDB function doesn't cause an error

await connectToDB();

if you didn't see the MongoDB connected then the problem is in the function

Check if the User object doesn't cause any error (if it's defined,...).

const userExists = await User.findOne({ 
    email: profile.email, 
});

Check the name of your google account does follow the rules that you have set in the models/user.js file :

// models/user.js file 
username: {
    type: String,
    required: [true, &#39;Username is required!&#39;],
    match: [
            /^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?&lt;![_.])$/,
            &quot;Username invalid, it should contain 8-20 alphanumeric letters and be unique!&quot;
    ] 
}

if it's not then you will find in the console the following error :

> Error: User validation failed: username: Username invalid, it should contain 8-20 alphanumberic letters
and be unique!

the name should be :

  1. contains only :
    • English alphabet A-Za-z (any characters different then that isn't
      allowed )
    • numbers 0-9
    • underscore _
    • point .
  2. number of characters is between 8 to 20
  3. all the combination of underscore and point aren't allowed to be inside the name

like :

example_.example

example_.example

example__example

example..example

答案4

得分: 0

在这个特定情况下,对于这个特定教程,您应该检查您的 Google 帐户中如何配置您的用户名。

https://myaccount.google.com/u/2/profile/name 中,在第三个字段中,您必须选择第一种方法来显示您的姓名给其他用户(因为 user.js 文件中的 "match" 方法不接受 "(", ")", 或者 "'" 字符在正则表达式中)。另外,请确保您的姓名已经配置好。

(对于那些没有遵循教程的人,user.js 文件如下所示:)

import { Schema, model, models } from "mongoose";

const userSchema = new Schema({
    email: {
        type: String,
        unique: [true, "Email already exists"],
        required: [true, "Email is required"],
    },

    username: {
        type: String,
        unique: [true, "Username already exists"],
        required: [true, "Username is required"],
        match: [/^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/, "Username is invalid, it should contain 8-20 alphanumeric characters and be unique!"],
    },

    image: {
        type: String,
    },
});

const User = models.User || model("User", userSchema);

export default User;

这是我遇到的问题(我的一个帐户可以工作,但其他帐户不行)。

英文:

In this specific case, for this particular tutorial, you should check how your username is configured in your Google account.

In https://myaccount.google.com/u/2/profile/name, in the third field, you must choose the first method to display your name to other users (because the "match" method in the user.js does not accept "(", ")", or '"' characters in the regex). Additionally, make sure your name is configured.

(For those who haven't followed the tutorial, the user.js file is as follows:)

import { Schema, model, models } from &quot;mongoose&quot;;

const userSchema = new Schema({
    email: {
        type: String,
        unique: [true, &quot;Email already exists&quot;],
        required: [true, &quot;Email is required&quot;],
    },

    username: {
        type: String,
        unique: [true, &quot;Username already exists&quot;],
        required: [true, &quot;Username is required&quot;],
        match: [/^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?&lt;![_.])$/, &quot;Username is invalid, it should contain 8-20 alphanumeric characters and be unique!&quot;],
    },

    image: {
        type: String,
    },
});

const User = models.User || model(&quot;User&quot;, userSchema);

export default User;

That was the issue I encountered (one of my accounts works, but not the others).

答案5

得分: 0

在我的情况下,对user.ts文件的更正有所帮助:

  name: {
    type: String,
    required: [ true, '需要用户名' ],
    match: [
      /^(?=.{8,150}$)(?![_.])(?!.*[_.]{2})[а-яА-Яa-zA-Z0-9._]+(?<![_.])$/,
      '用户名无效,应包含8-20个字母和数字,并且必须唯一!'
    ]
  },

问题在于我的用户名称是使用西里尔字母并且超过了20个字符。这个正则表达式的问题是:

/^(?=.{8,**20**}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$/,

已被替换为

/^(?=.{8,**150**}$)(?![_.])(?!.*[_.]{2})[**а-яА-Я**a-zA-Z0-9._]+(?<![_.])$/,

如果您在终端中收到以下错误消息,很可能您遇到了相同的问题:

用户验证失败:name:用户名无效,应包含8-20个字母和数字,并且必须唯一!

讨论和可能的其他解决方案

希望这对您的情况也有所帮助 I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

英文:

In my case, correction in user.ts helped:

  name: {
    type: String,
    required: [ true, &#39;Username is required&#39; ],
    match: [
      /^(?=.{8,150}$)(?![_.])(?!.*[_.]{2})[а-яА-Яa-zA-Z0-9._]+(?&lt;![_.])$/,
      &#39;Username invalid, it should contain 8-20 alphanumeric letters and be unique!&#39;
    ]
  },

Thing is that my user's name is in Cyrillic and more than 20 characters. Problem about this regular expression:

> /^(?=.{8,20}$)(?![.])(?!.*[.]{2})[a-zA-Z0-9.]+(?<![.])$/,

replaced by

> /^(?=.{8,150}$)(?![.])(?!.*[.]{2})[а-яА-Яa-zA-Z0-9.]+(?<![.])$/,

Most likely you have the same problem if you get the following error in the terminal:

> User validation failed: name: Username invalid, it should contain
> 8-20 alphanumeric letters and be unique!

discussions and possible other solutions

I hope this will help in your case too I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

答案6

得分: 0

我有同样的问题,在我的情况下,答案就是简单地删除<>符号,我一直在尝试:my_login_id:<my_password>,而应该是my_login_id:my_password。

英文:

I had the same problem, and in my case, the answer was as simple as removing the angle brackets "<"and ">" around <password>. I had been trying: my_login_id:<my_password>, when it should have been my_login_id:my_password.

答案7

得分: 0

// 移除/注释匹配键

username: {
    type: String,
    required: [true, '需要用户名']
    // match:[/^(?=.{8,150}$)(?![_.])(?!.*[_.]{2})[а-яА-Яa-zA-Z0-9._]+(?&lt;![_.])$/,
    // '用户名无效,应包含8-20个字母和数字,并且是唯一的!']
},
}
英文:

Just remove/comment the match key

username: {
    type: String,
    required: [true, &#39;Username is required&#39;]
    // match:[/^(?=.{8,150}$)(?![_.])(?!.*[_.]{2})[а-яА-Яa-zA-Z0-9._]+(?&lt;![_.])$/,
    // &#39;Username invalid, it should contain 8-20 alphanumeric letters and be unique!&#39;]
},
},

答案8

得分: 0

我遇到了相同的问题。解决方法是移除密码周围的尖括号。我试图使用 my_login_id:&lt;my_password&gt;,但应该是 my_login_id:my_password

希望这有所帮助 I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

英文:

I had the same problem. The solution was to remove the angle brackets around the password. I was trying to use my_login_id:&lt;my_password&gt;, but it should have been my_login_id:my_password.

Hope this helped I get http://localhost:3000/api/auth/error?error=AccessDenied in my NextJs application using google signin. Why?

huangapple
  • 本文由 发表于 2023年5月18日 00:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定