
import { observable } from 'mobx';
import cookie from 'utils/cookie';
import { ACCESS_TOKEN } from 'constants/CookieKeys';
import messageStore from 'stores/message';

// TODO
import fakeLogin from 'utils/fakeLogin';
import fakeAuth from 'utils/fakeAuth';

class AuthStore {
	@observable isFetching = false;

	getAccessToken() {
		return cookie.get(ACCESS_TOKEN);
	}

	async auth() {
		this.isFetching = true;
		let isOk = false;
		try {
			const { accessToken, expiresIn } = await fakeAuth({
				accessToken: this.getAccessToken(),
			});
			cookie.set(ACCESS_TOKEN, accessToken, { maxAge: expiresIn });
			this.accessToken = accessToken;
			isOk = true;

			__DEV__ && console.log('Auth success');
		}
		catch (err) {
			messageStore.push(err.message, 'error');
		}
		this.isFetching = false;
		return isOk;
	}

	async login(body) {
		this.isFetching = true;
		let isOk = false;
		try {
			const { accessToken, expiresIn } = await fakeLogin(body);
			cookie.set(ACCESS_TOKEN, accessToken, { maxAge: expiresIn });
			isOk = true;
		}
		catch (err) {
			messageStore.push(err.message, 'error');
		}
		this.isFetching = false;
		return isOk;
	}

	logout() {
		cookie.remove(ACCESS_TOKEN);
	}
}

export default new AuthStore();

@@ -1024,16 +1024,57 @@
 = true;%0A
+%09%09%09messageStore.push('%E7%99%BB%E5%BD%95%E6%88%90%E5%8A%9F', 'success');%0A
 %09%09%7D%0A%09%09ca

add login success message