Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error : request and redirection not using env variables. #33

Open
mderome opened this issue Apr 13, 2023 · 7 comments
Open

Error : request and redirection not using env variables. #33

mderome opened this issue Apr 13, 2023 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@mderome
Copy link
Collaborator

mderome commented Apr 13, 2023

bug

@mderome mderome added the bug Something isn't working label Apr 13, 2023
@ethan0905
Copy link
Owner

The error is related to the backend service not running properly when you sent the request.

@ethan0905
Copy link
Owner

Wait for the docker to finish the build, and to be able to read those logs, before interacting with the app.

[Nest] 33367  - 04/13/2023, 6:08:32 PM     LOG [NestFactory] Starting Nest application...
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] AppModule dependencies initialized +248ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] JwtModule dependencies initialized +3ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] PrismaModule dependencies initialized +2ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] AuthModule dependencies initialized +2ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] UserModule dependencies initialized +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [InstanceLoader] ChatModule dependencies initialized +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "create channel" message +10ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "sendMsgtoC" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "sendMsgtoC" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "join" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "quit" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "invit" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "ban" message +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "kick" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "is ban" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [WebSocketsController] ChatGateway subscribed to the "update" message +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RoutesResolver] AuthController {/auth}: +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/signin, POST} route +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth, GET} route +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/42/callback, GET} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/token, GET} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/42/logout, GET} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/2fa/status, GET} route +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/2fa/enable, POST} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/2fa/activated, POST} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/2fa/generate, POST} route +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/2fa/qrcode, GET} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/auth/2fa/verify, POST} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RoutesResolver] UserController {/users}: +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/users/me, GET} route +5ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/users, PATCH} route +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RoutesResolver] ChatController {/chat}: +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/chat/newchat, POST} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/chat/:id/deletechat, DELETE} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/chat/:id/newmsg, POST} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/chat/channels/:username, GET} route +1ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [RouterExplorer] Mapped {/chat/channels/users/:id, GET} route +0ms
[Nest] 33367  - 04/13/2023, 6:08:33 PM     LOG [NestApplication] Nest application successfully started +1ms

@ethan0905 ethan0905 added question Further information is requested wontfix This will not be worked on and removed bug Something isn't working labels Apr 13, 2023
@ethan0905 ethan0905 self-assigned this Apr 13, 2023
@mderome
Copy link
Collaborator Author

mderome commented Apr 13, 2023 via email

@mderome
Copy link
Collaborator Author

mderome commented Apr 13, 2023 via email

@ethan0905
Copy link
Owner

ethan0905 commented Apr 13, 2023

After our call, we understood that the issue was a bit different so i am changing the name to "Error : request and redirection not using env variables."

in frontend/login.tsx :

            <Button 
            text="Log in with 42"
            onClick={() => {
              window.open('https://api.intra.42.fr/oauth/authorize?client_id=u-s4t2ud-c3680374c7c94850b80d768576ab99300705487e1f5c7f758876aaf8fbf5fbdb&redirect_uri=http%3A%2F%2Flocalhost%3A3333%2Fauth%2F42%2Fcallback&response_type=code', "_self");
            }}
            />

in backend/auth.controller.ts :

  @Get('42/callback')
  async getToken( @Req() req: Request, @Res() res: Response) {

    if (!user.email)
    {
      res.redirect(`http://localhost:3000/login`); // change this
    }
    else {

      if (updatedUser.twoFactorActivated === false)
      {
        res.redirect(
          `http://localhost:3000/homepage`, // change this
          );
      }
      else if (updatedUser.twoFactorActivated === true)
      {
        res.redirect(
          `http://localhost:3000/2fa/verification`, // change this
          );
      }

      return { token: token, user: user };
    }
  }

in backend/auth.service.ts :

	async accessToken(req: string) {

		const client_id = "u-s4t2ud-c3680374c7c94850b....."; // change this
		const client_secret = "s-s4t2ud-448402f576330e3015892...." // change this

		try {
		  const response = await fetch("https://api.intra.42.fr/oauth/token", {
			method: "POST",
			headers: { "Content-Type": "application/x-www-form-urlencoded" },
			body: `grant_type=authorization_code&client_id=${client_id}&client_secret=${client_secret}&code=${req}&redirect_uri=http://localhost:3333/auth/42/callback`,
		  });
		  const data = await response.json();
		  
		  if (!data)
		  {
			throw new HttpException(
			  {
				status: HttpStatus.BAD_REQUEST,
				error: "Empty token"
			  },
			   HttpStatus.BAD_REQUEST); 
			};
		  return data;
		} catch (error) {
		  throw new HttpException(
			{
			  status: HttpStatus.BAD_REQUEST,
			  error: "Error while getting the user with token"},
			 HttpStatus.BAD_REQUEST); 
			};
		}

@ethan0905 ethan0905 changed the title bad redirect from network access Error : request and redirection not using env variables. Apr 13, 2023
@ethan0905 ethan0905 added enhancement New feature or request and removed question Further information is requested wontfix This will not be worked on labels Apr 13, 2023
@ethan0905
Copy link
Owner

Cluster Url example: http://e1r1p7.clusters.42paris.fr:3000/login

@ethan0905
Copy link
Owner

After our call, we understood that the issue was a bit different so i am changing the name to "Error : request and redirection not using env variables."

in frontend/login.tsx :

            <Button 
            text="Log in with 42"
            onClick={() => {
              window.open('https://api.intra.42.fr/oauth/authorize?client_id=u-s4t2ud-c3680374c7c94850b80d768576ab99300705487e1f5c7f758876aaf8fbf5fbdb&redirect_uri=http%3A%2F%2Flocalhost%3A3333%2Fauth%2F42%2Fcallback&response_type=code', "_self");
            }}
            />

in backend/auth.controller.ts :

  @Get('42/callback')
  async getToken( @Req() req: Request, @Res() res: Response) {

    if (!user.email)
    {
      res.redirect(`http://localhost:3000/login`); // change this
    }
    else {

      if (updatedUser.twoFactorActivated === false)
      {
        res.redirect(
          `http://localhost:3000/homepage`, // change this
          );
      }
      else if (updatedUser.twoFactorActivated === true)
      {
        res.redirect(
          `http://localhost:3000/2fa/verification`, // change this
          );
      }

      return { token: token, user: user };
    }
  }

in backend/auth.service.ts :

	async accessToken(req: string) {

		const client_id = "u-s4t2ud-c3680374c7c94850b....."; // change this
		const client_secret = "s-s4t2ud-448402f576330e3015892...." // change this

		try {
		  const response = await fetch("https://api.intra.42.fr/oauth/token", {
			method: "POST",
			headers: { "Content-Type": "application/x-www-form-urlencoded" },
			body: `grant_type=authorization_code&client_id=${client_id}&client_secret=${client_secret}&code=${req}&redirect_uri=http://localhost:3333/auth/42/callback`,
		  });
		  const data = await response.json();
		  
		  if (!data)
		  {
			throw new HttpException(
			  {
				status: HttpStatus.BAD_REQUEST,
				error: "Empty token"
			  },
			   HttpStatus.BAD_REQUEST); 
			};
		  return data;
		} catch (error) {
		  throw new HttpException(
			{
			  status: HttpStatus.BAD_REQUEST,
			  error: "Error while getting the user with token"},
			 HttpStatus.BAD_REQUEST); 
			};
		}

Fixed all of these hard api call and stuff. From backend and from frontend.
Last thing is to try connection using the 42 url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants