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

Add gitignore to remove all unnessisary files #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.next
package-lock.json
sessions
yarn.lock
.env
18 changes: 9 additions & 9 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
apiKey: "",
authDomain: "-.firebaseapp.com",
databaseURL: "https://-.firebaseio.com",
projectId: "-",
storageBucket: "-.appspot.com",
messagingSenderId: "",
appId: "1::web:"
}

apiKey: process.env.APIKEY,
authDomain: process.env.AUTHDOMAIN,
databaseURL: process.env.DATABASEURL,
projectId: process.env.PROJECTID,
storageBucket: process.env.STORAGEBUCKET,
messagingSenderId: process.env.MSID,
appId: process.env.APPID,
measurementId: process.env.MESID
}
33 changes: 2 additions & 31 deletions pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import Document, { Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/core/styles';
import theme from '../src/theme';
Expand All @@ -9,10 +8,6 @@ export default class MyDocument extends Document {
<html lang="en">
<Head>
<meta charSet="utf-8" />
<meta
name="viewport"
content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
/>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link
Expand All @@ -29,43 +24,19 @@ export default class MyDocument extends Document {
}
}

MyDocument.getInitialProps = async ctx => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render

// Render app and page and get the context of the page with collected side effects.
MyDocument.getInitialProps = async (ctx) => {
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;

ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheets.collect(<App {...props} />),
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});

const initialProps = await Document.getInitialProps(ctx);

return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
};
80 changes: 47 additions & 33 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const CLIENT_ID = "";
const CLIENT_SECRET = "--";
const redirect = encodeURIComponent('');
const CLIENT_ID = process.env.CLID;
const CLIENT_SECRET = process.env.CLSC;
const redirect = encodeURIComponent('https://spartalytics.gam3rr.me/api/callback');

// async/await error catcher
const catchAsync = fn => (
Expand All @@ -30,9 +30,9 @@ app.prepare().then(() => {
server.use(bodyParser.json())
server.use(
session({
secret: 'geheimnis',
secret: process.env.SECR,
saveUninitialized: true,
store: new FileStore({ secret: 'geheimnis' }),
store: new FileStore({ secret: process.env.SECR }),
resave: false,
rolling: true,
httpOnly: true,
Expand All @@ -41,37 +41,51 @@ app.prepare().then(() => {
)

server.get('/api/login', (req, res) => {
res.redirect(`https://discordapp.com/api/oauth2/authorize?client_id=&redirect_uri=callback&response_type=code&scope=identify`)
res.redirect(`https://discord.com/api/oauth2/authorize?client_id=1194920087887548477&response_type=code&redirect_uri=https%3A%2F%2Fspartalytics.gam3rr.me%2Fapi%2Fcallbacks&scope=identify`)
})

server.post('/api/callbacks', (req, res) => {
if (!req.body) throw new Error('NoCodeProvided');
const code = req.body.code;
//console.log(code)
const creds = btoa(`${CLIENT_ID}:${CLIENT_SECRET}`);

//Initial Call to Discord Oauth Server
fetch(`https://discordapp.com/api/oauth2/token?grant_type=authorization_code&code=${code}&redirect_uri=${redirect}`,
{
server.get('/api/callbacks', async (req, res) => {
const code = req.query.code;

if (!code) {
throw new Error('NoCodeProvided');
}

try {
var body = {
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': redirect,
};

var site = await fetch("https://discord.com/api/v9/oauth2/token", {
method: 'POST',
headers: {
Authorization: `Basic ${creds}`,
}
}).then(response => response.json().then(callbackJson => {
const access_token = callbackJson.access_token
fetch('http://discordapp.com/api/users/@me',
{
method: 'POST',
headers: {
Authorization: `Bearer ${access_token}`
}
}).then(response => response.json().then(refreshTokenJson => {
req.session.user = refreshTokenJson
res.json(refreshTokenJson)
})
)
}))
})
body: JSON.stringify(body),
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
});
var response = await site.json();
var accessToken = response['access_token'];
const fetchDiscordUserInfo = await fetch('http://discordapp.com/api/users/@me', {
headers: {
Authorization: `Bearer ${accessToken}`,
}
});

const refreshTokenJson = await fetchDiscordUserInfo.json();

// Save user information to session
req.session.user = refreshTokenJson;

// Respond with the user information
res.json(refreshTokenJson);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal Server Error' });
}
});


server.get('/api/logout', (req, res) => {
req.session.destroy((err) => {
Expand Down
4 changes: 2 additions & 2 deletions src/theme.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createMuiTheme } from '@material-ui/core/styles';
import { createTheme } from '@material-ui/core/styles';
import { red } from '@material-ui/core/colors';

// Create a theme instance.
const theme = createMuiTheme({
const theme = createTheme({
palette: {
primary: {
main: '#335A40',
Expand Down
Loading