我有一个使用Rails,React和Webpacker构建的应用程序。我使用的是基于令牌的身份验证,因此,当用户创建帐户或登录时,会在数据库中为其分配令牌。然后,此令牌用于访问用户的个人资料。用户进行身份验证后,配置文件页面加载没有问题,但是出现Rails错误,提示 static void Main(string[] args)
{
Menu();
Console.ReadKey();
}
static void Menu()
{
int userChoice = MenuChoice();
if (userChoice == 4)
{
Console.WriteLine("Thank you, Goodbye!");
}
while (userChoice != 4)
{
if (userChoice == 1)
{
Console.WriteLine("Welcome to Slot Machine!\n");
SlotMachineGame();
}
if (userChoice == 2)
{
Console.WriteLine("Welcome to Dice!");
DiceGame();
}
if (userChoice == 3)
{
Console.WriteLine("Welcome to Roulette Wheel!\n");
RouletteWheel();
}
userChoice = MenuChoice();
}
}
static int MenuChoice()
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("Welcome to Jolly Jackpot Land!");
Console.WriteLine("You can play our two traditional games: Slot Machine and Dice.");
Console.WriteLine("Or.. Try the all new Roulette Wheel!");
Console.WriteLine("Further rules for each game will be displayed when chosen.");
Console.WriteLine("\nPress 1 to play Slot Machine");
Console.WriteLine("\nPress 2 to play Dice");
Console.WriteLine("\nPress 3 to play Roulette Wheel");
Console.WriteLine("\nPress 4 to Exit");
Console.WriteLine("----------------------------------------------------------");
string choice = Console.ReadLine();
Console.WriteLine();
while (!(choice == "1" || choice == "2" || choice == "3"))
{
Console.WriteLine("Please try again");
Console.WriteLine("Press 1 to play Slot Machine");
Console.WriteLine("Press 2 to play Dice");
Console.WriteLine("Press 3 to exit");
choice = Console.ReadLine();
}
return int.Parse(choice);
}
static void SlotMachineGame()
{
Console.WriteLine("The objective of this game is to match words. Rewards listed below:\n");
Console.WriteLine("No words match: Lose the Gil you risked");
Console.WriteLine("Two words match: win DOUBLE the amount of Gil risked");
Console.WriteLine("Three words match: win TRIPLE the amount of Gil risked\n");
Random rand = new Random();
List<string> list = new List<string> { "Elephant", "Computer", "Football", "Resume", "Capstone", "Crimson" }; // List of words being randomly chosen
List<string> wordsChosen = new List<string>(); // storing three random words here
Console.WriteLine("Enter the amount of Gil you would like to risk: "); // Asking user bet amount and storing bet amount and potential winnings
double gil = Convert.ToDouble(Console.ReadLine());
double winningAmountSlot = 0;
for (int i = 1; i <= 3; i++)
{
string word = list[rand.Next(list.Count)];
wordsChosen.Add(word);
Console.Write(word + " ");
}
Console.WriteLine();
if (wordsChosen[0] == wordsChosen[1] && wordsChosen[1] == wordsChosen[2])
{
winningAmountSlot = gil * 3;
}
else if (wordsChosen[0] == wordsChosen[1] || wordsChosen[0] == wordsChosen[2] || wordsChosen[1] == wordsChosen[2])
{
winningAmountSlot = gil * 2;
}
else
{
winningAmountSlot = 0;
}
Console.Write("You won: " + winningAmountSlot + " Gil from playing Slot Machine\n");
}
,然后它从我的registration_controller中引用了这一行:Couldn't find Registration
。
当从后端接收到令牌时,我已经尝试在localStorage中设置令牌,并且当页面使用registration = Registration.find_by_auth_token!(request.headers[:token])
钩子加载顶级组件时,我尝试从localStorage调用令牌,但是它没有在出现此错误页面之前,我的React应用似乎还没有开始渲染。当我收到此错误时,我添加到React应用程序的Console.log语句都不会出现在控制台中。
registration_controller.rb
useEffect
auth.js(操作)
class RegistrationController < ApplicationController
skip_before_action :verify_authenticity_token
def index; end
def custom
user = Registration.create!(registration_params)
render json: { token: user.auth_token, id: user.id }
end
def profile
registration = Registration.find_by_auth_token!(request.headers[:token])
render json: {
registration: { username: registration.username, email: registration.email, name: registration.name }
}
end
private
def registration_params
params.require(:registration).permit(:username, :email, :password, :name)
end
end
我希望该令牌始终在Rails侧的Request对象的Headers中可用,以便在用户刷新窗口时可以显示配置文件页面。
答案 0 :(得分:0)
如果您的标头是“令牌”,rails会将其转换为“ HTTP_TOKEN”,运行puts request.headers.inspect
来查看标头对象中实际包含的内容,我想您也必须使用字符串而不是符号(标头['HTTP_TOKEN']代替标题[:http_token])
答案 1 :(得分:0)
我能够通过添加一些辅助函数并在React端更改一些逻辑来解决此问题。我在页面加载时,用户登录时和用户注销时开始在本地存储中设置/获取/清除令牌,最终这是在前端管理身份验证状态的更好方法