From 25d876d22159830ec74703e1c19b44cd2208138b Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Thu, 10 Sep 2020 18:42:57 +0200 Subject: [PATCH] Add support for PG SSO --- src/api/user.ts | 19 ++++++++++++++++--- src/pages/user/login.tsx | 32 ++++++++++++++++++++++++-------- src/routing.tsx | 8 +++++++- webpack.config.js | 5 +++-- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/api/user.ts b/src/api/user.ts index ba70d5f..e930190 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -1,9 +1,22 @@ import { axios } from "@/api/index"; +import { query, route } from "@/routing"; -const AUTHORIZE_ENDPOINT = "/access/login" +const LOGIN_ENDPOINT = "/access/login" -export async function authorize(code: string): Promise { - const response = await axios.get(AUTHORIZE_ENDPOINT, { params: { code }}); +const CLIENT_ID = process.env.LOGIN_CLIENT_ID || "PraktykiClientId"; +const AUTHORIZE_URL = process.env.AUTHORIZE || "https://logowanie.pg.edu.pl/oauth2.0/authorize"; + +export async function login(code: string): Promise { + const response = await axios.get(LOGIN_ENDPOINT, { params: { code }}); return response.data; } + +export function getAuthorizeUrl() { + return query(AUTHORIZE_URL, { + response_type: "code", + scope: "user_details", + client_id: CLIENT_ID, + redirect_uri: window.location.origin + route("user_login") + "/check/pg", + }) +} diff --git a/src/pages/user/login.tsx b/src/pages/user/login.tsx index 89fa966..f7a9099 100644 --- a/src/pages/user/login.tsx +++ b/src/pages/user/login.tsx @@ -1,8 +1,8 @@ import React, { Dispatch } from "react"; import { Page } from "@/pages/base"; -import { Button, Container, Typography } from "@material-ui/core"; +import { Button, Container } from "@material-ui/core"; import { Action, useDispatch } from "@/state/actions"; -import { useHistory } from "react-router-dom"; +import { Route, Switch, useHistory, useLocation, useRouteMatch } from "react-router-dom"; import { route } from "@/routing"; import { useVerticalSpacing } from "@/styles"; import { AppState } from "@/state/reducer"; @@ -10,9 +10,10 @@ import { AppState } from "@/state/reducer"; import api from "@/api"; import { UserActions } from "@/state/actions/user"; import { sampleStudent } from "@/provider/dummy"; +import { getAuthorizeUrl } from "@/api/user"; const authorizeUser = async (dispatch: Dispatch, getState: () => AppState): Promise => { - const token = await api.user.authorize("test"); + const token = await api.user.login("test"); dispatch({ type: UserActions.Login, @@ -24,6 +25,8 @@ const authorizeUser = async (dispatch: Dispatch, getState: () => AppStat export const UserLoginPage = () => { const dispatch = useDispatch(); const history = useHistory(); + const match = useRouteMatch(); + const query = new URLSearchParams(useLocation().search); const handleSampleLogin = async () => { await dispatch(authorizeUser); @@ -31,16 +34,29 @@ export const UserLoginPage = () => { history.push(route("home")); } + const handlePgLogin = async () => { + history.push(route("user_login") + "/pg"); + } + const classes = useVerticalSpacing(3); return - Tu miało być przekierowanie do logowania PG... + Zaloguj się - - ... ale wciąż czekamy na dostęp :( - - + + + + + + + + + (window.location.href = getAuthorizeUrl())} /> + + Kod: { query.get("code") } + + ; } diff --git a/src/routing.tsx b/src/routing.tsx index f57249d..f4f5fd1 100644 --- a/src/routing.tsx +++ b/src/routing.tsx @@ -25,7 +25,7 @@ export const routes: Route[] = [ { name: "internship_plan", path: "/internship/plan", exact: true, content: () => }, // user - { name: "user_login", path: "/user/login", exact: true, content: () => }, + { name: "user_login", path: "/user/login", content: () => }, // fallback route for 404 pages { name: "fallback", path: "*", content: () => } @@ -44,3 +44,9 @@ export function route(name: string, params: URLParams = {}) { return prepare(url, params) } + +export const query = (url: string, params: URLParams) => { + const query = Object.entries(params).map(([ name, value ]) => `${name}=${encodeURIComponent(value)}`).join("&"); + + return url + (query.length > 0 ? `?${query}` : ''); +} diff --git a/webpack.config.js b/webpack.config.js index c3636bf..94454fc 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -51,11 +51,12 @@ const config = { ], devServer: { contentBase: path.resolve("./public/"), - port: 3000, - host: 'system-praktyk-front.localhost', + port: 443, + host: 'system-praktyk.stg.kadet.net', disableHostCheck: true, historyApiFallback: true, overlay: true, + https: true, proxy: { "/api": { target: "http://system-praktyk-front.localhost:8080/",