sungjin

Make new Frontend with NextJS, with Auth

This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
## Getting Started
First, run the development server:
```bash
npm run dev
# or
yarn dev
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
## Learn More
To learn more about Next.js, take a look at the following resources:
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
## Deploy on Vercel
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
module.exports = {
reactStrictMode: true,
}
This diff could not be displayed because it is too large.
{
"name": "learn_with_code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"name": "learn-in-web",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm run build:live",
"build": "tsc -p .",
"build:live": "nodemon --watch 'src/**/*.ts' --exec \"ts-node\" src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^16.11.6",
"nodemon": "^2.0.14",
"ts-node": "^10.4.0",
"typescript": "^4.4.4"
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"express": "^4.17.1"
"@heroicons/react": "^1.0.5",
"axios": "^0.24.0",
"next": "12.0.4",
"next-auth": "^3.29.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"semantic-ui-react": "^2.0.4",
"swr": "^1.0.1"
},
"devDependencies": {
"autoprefixer": "^10.4.0",
"eslint": "7.32.0",
"eslint-config-next": "12.0.4",
"postcss": "^8.3.11",
"tailwindcss": "^2.2.19"
}
}
......
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
No preview for this file type
<svg width="283" height="64" viewBox="0 0 283 64" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/>
</svg>
\ No newline at end of file
import { useSession } from 'next-auth/client';
import axios from 'axios';
import { SERVER_BASE_URL } from '..';
// login request to the server with axios and next-auth
export const login = async (email, password) => {
const response = await axios.post(`${SERVER_BASE_URL}/auth/signin` , {
email,
password,
});
if (response.status !== 200 && response.status !== 201) {
throw new Error('Login failed!');
}
console.log(response.data.access_token)
useSession.accessToken = response.data.access_token;
return response.data;
}
export const signup = async (name, email, password) => {
const response = await axios.post(`${SERVER_BASE_URL}/auth/signup`, {
name,
email,
password,
});
if (response.status !== 200 && response.status !== 201) {
throw new Error('Signup failed!');
}
useSession.accessToken = response.data.access_token;
return response.data;
}
export const logout = async () => {1
useSession.accessToken = null;
return true;
}
export const getUser = async () => {
const response = await axios.get(`${SERVER_BASE_URL}/auth/validate`, {
token: useSession.accessToken,
});
if (response.status !== 200 && response.status !== 201) {
throw new Error('Failed to get user!');
}
return response.data;
}
export const refreshToken = async () => {
const response = await axios.post(`${SERVER_BASE_URL}/auth/refresh`, {
token: useSession.accessToken,
});
if (response.status !== 200 && response.status !== 201) {
throw new Error('Failed to refresh token!');
}
useSession.accessToken = response.data.access_token;
return response.data;
}
export const validateToken = async () => {
const response = await axios.post(`${SERVER_BASE_URL}/auth/validate`, {
token: useSession.accessToken,
});
if (response.status !== 200 && response.status !== 201) {
return false;
}
return true;
}
export const getToken = () => {
return useSession.accessToken;
}
\ No newline at end of file
export * from './auth';
\ No newline at end of file
export const SERVER_BASE_URL = 'http://localhost:4000';
\ No newline at end of file
export * from './post'
\ No newline at end of file
import auth from '../auth'
import axios from 'axios';
import { SERVER_BASE_URL } from '..';
export const newPost = async (title, content, privat) => {
if (!auth.validateToken()) {
throw new Error("plz login");
}
const response = await axios.post(`${SERVER_BASE_URL}/post/new`, {
token: auth.getToken(),
title,
content,
privat,
});
if (response.status !== 200) {
throw new Error('Failed to create new post!');
}
return response.data;
}
export const getPostbyId = async (id) => {
const response = await axios.get(`${SERVERBASE_URL}/post/${id}`);
if (response.status !== 200) {
throw new Error('Failed to get post!');
}
return response.data;
}
\ No newline at end of file
function Footer() {
return (
<div>Copyright @ SungJin. All right reserved.</div>
)
}
export default Footer
\ No newline at end of file
import {
HomeIcon,
SearchIcon,
UserIcon,
CubeIcon,
MailIcon,
StarIcon
} from "@heroicons/react/outline"
import { useSession } from "next-auth/client"
import HeaderItem from "./HeaderItem"
import Islogin from "./islogin"
import Link from "next/link"
function Header() {
return (
<header className="flex flex-col sm:flex-row m-5 justify-between items-center">
<Link href="/" passHref>
<div className="flex cursor-pointer transform hover:scale-105">
<CubeIcon className="h-20" />
<p className="text-7xl font-extrabold" >Learn In Web</p>
</div></Link>
<div className="flex flex-grow justify-evenly max-w-sm">
<HeaderItem title='HOME' Icon={HomeIcon} />
<HeaderItem title='STAR' Icon={StarIcon} />
<HeaderItem title='SEARCH' Icon={SearchIcon} />
<HeaderItem title='ACCOUNT' Icon={UserIcon} />
</div>
<Islogin />
</header>
)
}
export default Header
\ No newline at end of file
function HeaderItem({ Icon, title }) {
return (
<div className="flex flex-col items-center cursor-pointer group w-12 sm:w-20 hover:text-white">
<Icon className="h-8 mb-1 group-hover:animate-bounce"/>
<p className="opacity-0 group-hover:opacity-100 tracking-widest">{title}</p>
</div>
)
}
export default HeaderItem
\ No newline at end of file
import router from "next/router"
export default function Nav() {
return (
<nav className="navbar">
<div className="flex px-10 sm:px-20 text-2xl whitespace-nowrap
space-x-10 sm:space-x-20">
<a herf="" className="last:pr-24 cursor-pointer transition
duration-100 transform hover:scale-125 hover:text-white
active:text-blue-500">난이도 </a>
<a herf="" className="last:pr-24 cursor-pointer transition
duration-100 transform hover:scale-125 hover:text-white
active:text-blue-500">난이도 </a>
<a herf="" className="last:pr-24 cursor-pointer transition
duration-100 transform hover:scale-125 hover:text-white
active:text-blue-500">난이도 </a>
<a herf="" className="last:pr-24 cursor-pointer transition
duration-100 transform hover:scale-125 hover:text-white
active:text-blue-500">북마크</a>
</div>
</nav>
)
}
\ No newline at end of file
import { useSession } from "next-auth/client"
import Link from "next/link"
function Islogin() {
if (useSession.accessToken) {
return (
<LogoutRedirect />
)
} else {
return (
<LoginRedirect />
)
}
}
function LogoutRedirect() {
return (
<div>
<Link href="/logout"><a className="flex flex-grow justify-evenly">로그아웃</a></Link>
</div>
)
}
function LoginRedirect () {
return (
<div>
<Link href="/login"><a className="flex flex-grow justify-evenly">로그인</a></Link>
<Link href="/signup"><a className="flex flex-grow justify-evenly">회원가입</a></Link>
</div>
)
}
export default Islogin
\ No newline at end of file
const _PORT = 3000;
import * as express from "express";
import {Runner} from "./runner";
const app = express.default();
app.use(express.json());
/*RESTful API*/
app.post("/runcode", function (req, res) {
var run = new Runner(req);
run.run();
res.send(run.output);
});
app.listen(_PORT, () => {
console.log("server started at port " + _PORT);
});
import '../styles/globals.css'
import Header from '../components/Header'
import Nav from '../components/Nav'
import Footer from '../components/Footer'
function MyApp({ Component, pageProps }) {
return (
<div>
<Header />
<Nav />
<Component {...pageProps} />
<Footer />
</div>
)
}
export default MyApp
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
export default function handler(req, res) {
res.status(200).json({ name: 'John Doe' })
}
import {Header} from 'semantic-ui-react'
import Head from 'next/head'
export default function Home() {
return (
<div className=''>
<Head>
<title>Learn In Web</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
{/* 기본 컨텐츠 */}
<Header as='h3' textAlign='center'>인기 문제</Header>
</div>
)
}
\ No newline at end of file
import { useRouter } from "next/router";
import * as auth from "../api/auth";
export default function Login() {
const router = useRouter();
return (
<div>
<h1>Login</h1>
<form onSubmit={handleSubmit}>
<label htmlFor="email">Email</label>
<input type="email" id="email" />
<label htmlFor="password">Password</label>
<input type="password" id="password" />
<button type="submit">Login</button>
</form>
</div>
);
async function handleSubmit(event) {
event.preventDefault();
const form = event.target;
const email = form.email.value;
const password = form.password.value;
const login = await auth.login(email, password);
if(!login) {
alert("Login failed");
}
return router.push("/");
}
}
import { useRouter } from "next/dist/client/router"
import { getPostbyId } from "../../api/post"
export default function Post() {
const router = useRouter()
const { id } = router.query
const post = getPostbyId(id)
return (
<div>
<p>{post.title}</p>
<p>{post.body}</p>
</div>
)
}
\ No newline at end of file
import * as auth from "../api/auth";
import push from "next/router";
export default function Signup() {
return (
<div>
<h1>Signup</h1>
<form onSubmit={handleSubmit}>
<label htmlFor="name">Name</label>
<input type="text" id="name" />
<label htmlFor="email">Email</label>
<input type="email" id="email" />
<label htmlFor="password">Password</label>
<input type="password" id="password" />
<button type="submit">Signup</button>
</form>
</div>
);
}
function handleSubmit(event) {
event.preventDefault();
const form = event.target;
const name = form.name.value;
const email = form.email.value;
const password = form.password.value;
console.log(name, email, password);
const signup = auth.signup(name, email, password);
if(!signup) {
alert("Signup failed");
}
return push("/");
}
\ No newline at end of file
import * as child_process from "child_process";
import * as fs from "fs";
export class Runner {
type: string;
input: Array<string>;
output: Array<string> = [];
time: Date = new Date();
constructor(req: any) {
this.type = req.body.type;
this.input = req.body.input;
fs.writeFileSync("tmp." + this.type, req.body.code);
}
run() {
switch (this.type) {
case "c": {
this.c();
}
case "c++": {
this.cpp();
}
case "js": {
this.js();
}
case "go": {
this.go();
}
case "ts": {
this.ts();
}
}
}
c() {
if ((this.input = [])) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
}
cpp() {
if (this.input = []) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("gcc", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stderr);
const result = child_process.spawnSync("~/OSS/main/a.out", {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(result.stdout);
this.output.push(fix(result.stdout));
}
}
js() {
if (this.input = []) {
const test = child_process.spawnSync("node", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("node", ["~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
}
go() {
if (this.input = []) {
const test = child_process.spawnSync("go", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("go", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
}
ts() {
if (this.input = []) {
const test = child_process.spawnSync("ts-node", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
for (let ip of this.input) {
const test = child_process.spawnSync("ts-node", ["run", "~/OSS/main/tmp.c"], {
encoding: "utf8",
shell: true,
input: ip,
});
console.log(test.stdout);
this.output.push(fix(test.stdout));
}
}
}
const fix = (a: any): string => {
if (typeof a === "string") {
return a;
}
return "";
};
.container {
padding: 0 2rem;
}
.main {
min-height: 100vh;
padding: 4rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.footer {
display: flex;
flex: 1;
padding: 2rem 0;
border-top: 1px solid #eaeaea;
justify-content: center;
align-items: center;
}
.footer a {
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
}
.title a {
color: #0070f3;
text-decoration: none;
}
.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}
.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}
.title,
.description {
text-align: center;
}
.description {
margin: 4rem 0;
line-height: 1.5;
font-size: 1.5rem;
}
.code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
}
.card {
margin: 1rem;
padding: 1.5rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
max-width: 300px;
}
.card:hover,
.card:focus,
.card:active {
color: #0070f3;
border-color: #0070f3;
}
.card h2 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
}
.card p {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
}
.logo {
height: 1em;
margin-left: 0.5rem;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
body {
@apply bg-[#06202A] text-gray-300
}
}
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
module.exports = {
mode: 'jit',
purge: ['./src/**/*.{js,ts,jsx,tsx}'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
This diff is collapsed. Click to expand it.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="wrapper">
<div class="header">Code in Web</div>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</div>
</body>
</html>