0
0
Fork 0
This commit is contained in:
xeltica 2021-09-01 21:32:45 +09:00
parent 347a5680ae
commit ba40952a09
7 changed files with 99 additions and 49 deletions

View file

@ -2,4 +2,20 @@ import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { App } from './App';
// cookieにトークンが入ってたらlocalStorageに移し替える
const token = document.cookie
.split('; ')
.find(row => row.startsWith('token'))
?.split('=')[1];
console.log(document.cookie);
console.log(token);
if (token) {
localStorage['token'] = token;
// 今の所はcookieをトークン以外に使用しないため全消去する
// もしcookieの用途が増えるのであればここを良い感じに書き直す必要がある
document.cookie = '';
}
ReactDOM.render(<App/>, document.getElementById('app'));

View file

@ -0,0 +1,27 @@
import React, { useEffect, useState } from 'react';
import { Header } from '../components/Header';
export const IndexSessionPage: React.VFC = () => {
const token = localStorage['token'];
const [session, setSession] = useState<Record<string, any> | null>(null);
useEffect(() => {
fetch(`//${location.host}/api/v1/session`, {
headers: {
'Authorization': `Bearer ${token}`,
},
}).then(s => s.json())
.then(setSession);
}, []);
return (
<>
<Header>
<article className="mt-4">
{session?.username}
</article>
</Header>
</>
);
};

View file

@ -1,41 +1,10 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { Ranking } from '../components/Ranking';
import { LoginForm } from '../components/LoginForm';
import { DeveloperInfo } from '../components/DeveloperInfo';
import { HashtagTimeline } from '../components/HashtagTimeline';
import { Header } from '../components/Header';
import { IndexSessionPage } from './index.session';
import { IndexWelcomePage } from './index.welcome';
export const IndexPage: React.VFC = () => {
return (
<>
<Header>
<article className="mt-4">
<p>
Misskeyは楽しいものです1
</p>
<p>
</p>
</article>
<LoginForm />
</Header>
<article className="xarticle card ghost">
<div className="body">
<h1 className="mb-1"></h1>
<Ranking limit={10} />
<Link to="/ranking"></Link>
</div>
</article>
<article className="xarticle mt-4 row">
<div className="col-12 pc-6 card ghost">
<div className="body"><DeveloperInfo/></div>
</div>
<div className="col-12 pc-6 card ghost">
<div className="body"><HashtagTimeline hashtag="misshaialert"/></div>
</div>
</article>
</>
);
const token = localStorage.getItem('token');
return token ? <IndexSessionPage /> : <IndexWelcomePage />;
};

View file

@ -0,0 +1,37 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { Ranking } from '../components/Ranking';
import { LoginForm } from '../components/LoginForm';
import { DeveloperInfo } from '../components/DeveloperInfo';
import { HashtagTimeline } from '../components/HashtagTimeline';
import { Header } from '../components/Header';
export const IndexWelcomePage: React.VFC = () => {
return (
<>
<Header>
<article className="mt-4">
<p>Misskeyは楽しいものです1</p>
<p></p>
</article>
<LoginForm />
</Header>
<article className="xarticle card ghost">
<div className="body">
<h1 className="mb-1"></h1>
<Ranking limit={10} />
<Link to="/ranking"></Link>
</div>
</article>
<article className="xarticle mt-4 row">
<div className="col-12 pc-6 card ghost">
<div className="body"><DeveloperInfo/></div>
</div>
<div className="col-12 pc-6 card ghost">
<div className="body"><HashtagTimeline hashtag="misshaialert"/></div>
</div>
</article>
</>
);
};

View file

@ -252,7 +252,7 @@ async function login(ctx: Context, user: Record<string, unknown>, host: string,
const misshaiToken = await updateUsersMisshaiToken(u);
ctx.cookies.set('token', misshaiToken, { signed: true });
ctx.cookies.set('token', misshaiToken, { signed: false, httpOnly: false });
// await ctx.render('logined', { user: u });
ctx.redirect('/');

View file

@ -25,11 +25,12 @@ export default (): void => {
routePrefix: '/api/v1',
defaultErrorHandler: false,
currentUserChecker: async ({ request }: Action) => {
const authorization: string | null = request.headers['Authorization'];
const { authorization } = request.header;
if (!authorization || !authorization.startsWith('Bearer ')) return null;
const token = authorization.split(' ')[1];
return getUserByMisshaiToken(token);
const token = authorization.split(' ')[1].trim();
const user = await getUserByMisshaiToken(token);
return user;
},
});

View file

@ -25,7 +25,7 @@ html
align-items: center;
justify-content: center;
}
body
body.dark
#app: .loading Loading...
script(src=`/assets/fe.${version}.js`)
script(src=`/assets/fe.${version}.js` async defer)