<commit_before>import { observable } from 'mobx';
import cookie from 'utils/cookie';
import { ACCESS_TOKEN } from 'constants/CookieKeys';
import { message } from 'antd';
import getRequest from 'utils/getRequest';
import { isString } from 'lodash';
import deprecated from 'utils/deprecated';
import showError from 'utils/showError';

const verifyAndSaveAccessToken = (token, maxAge) => {
	if (!isString(token)) {
		throw new Error(`"accessToken" is INVALID, received "${token}"`);
	}

	if (!+maxAge) {
		__DEV__ && console.warn('Missing `expiresIn` attribute.');
	}

	maxAge && cookie.set(ACCESS_TOKEN, token, { maxAge });
};

class AuthStore {
	@observable isFetching = true;
	@observable accessToken = cookie.get(ACCESS_TOKEN);

	init(config) {
		this._apiConfig = config.api;
		this._config = config.auth;
		this._request = getRequest(config).clone(this._config.basePath);
	}

	// TODO: Deprecated
	getAccessToken() {
		deprecated('getAccessToken()', 'accessToken');
		return this.accessToken;
	}

	async auth() {
		this.isFetching = true;
		let isOk = false;
		try {
			const options = { url: this._config.getUserPath };
			if (this.accessToken) {
				const { accessTokenLocation } = this._apiConfig;
				options[accessTokenLocation] = {
					[this._apiConfig.accessTokenName]: this.accessToken,
				};
			}

			const res = await this._request.fetch(options);
			const { accessToken, expiresIn } = this._config.mapOnGetUserResponse(res);
			verifyAndSaveAccessToken(accessToken, expiresIn);
			this.accessToken = accessToken;
			isOk = true;
		} catch (err) {
			showError('登录失效', err);
		}
		this.isFetching = false;
		return isOk;
	}

	async login(body) {
		this.isFetching = true;
		let isOk = false;

		try {
			const res = await this._request.fetch({
				url: this._config.loginPath,
				method: 'POST',
				body,
			});
			const { accessToken, expiresIn } = this._config.mapOnLoginResponse(res);
			verifyAndSaveAccessToken(accessToken, expiresIn);
			this.accessToken = accessToken;

			isOk = true;
			message.success('登录成功');
		} catch (err) {
			showError('登录失效', err);
		}
		this.isFetching = false;
		return isOk;<commit_msg>prefer expiresInMilliseconds than expiresIn in AuthStore<commit_after>import { observable } from 'mobx';
import cookie from 'utils/cookie';
import { ACCESS_TOKEN } from 'constants/CookieKeys';
import { message } from 'antd';
import getRequest from 'utils/getRequest';
import { isString } from 'lodash';
import deprecated from 'utils/deprecated';
import showError from 'utils/showError';

const verifyAndSaveAccessToken = (authRes = {}) => {
	const { accessToken, expiresIn, expiresInMilliseconds } = authRes;

	if (!isString(accessToken)) {
		throw new Error(`"accessToken" is INVALID, received "${accessToken}"`);
	}

	const maxAge = expiresInMilliseconds || (expiresIn && expiresIn * 1000) || 0;

	if (!+maxAge) {
		__DEV__ && console.warn('Missing `expiresIn` attribute.');
	}

	maxAge && cookie.set(ACCESS_TOKEN, accessToken, { maxAge });
};

class AuthStore {
	@observable isFetching = true;
	@observable accessToken = cookie.get(ACCESS_TOKEN);

	init(config) {
		this._apiConfig = config.api;
		this._config = config.auth;
		this._request = getRequest(config).clone(this._config.basePath);
	}

	// TODO: Deprecated
	getAccessToken() {
		deprecated('getAccessToken()', 'accessToken');
		return this.accessToken;
	}

	async auth() {
		this.isFetching = true;
		let isOk = false;
		try {
			const options = { url: this._config.getUserPath };
			if (this.accessToken) {
				const { accessTokenLocation } = this._apiConfig;
				options[accessTokenLocation] = {
					[this._apiConfig.accessTokenName]: this.accessToken,
				};
			}

			const res = await this._request.fetch(options);
			const authRes = this._config.mapOnGetUserResponse(res);
			verifyAndSaveAccessToken(authRes);
			this.accessToken = authRes.accessToken;
			isOk = true;
		} catch (err) {
			showError('登录失效', err);
		}
		this.isFetching = false;
		return isOk;
	}

	async login(body) {
		this.isFetching = true;
		let isOk = false;

		try {
			const res = await this._request.fetch({
				url: this._config.loginPath,
				method: 'POST',
				body,
			});

			const authRes = this._config.mapOnLoginResponse(res);
			verifyAndSaveAccessToken(authRes);
			this.accessToken = authRes.accessToken;

			isOk = true;
			message.success('登录成功');
		} catch (err) {
			showError('登录失效', err);
		}
		this.isFetching = false;
		return isOk;
{'fraction_non_alphanumeric': 0.13406940063091483, 'fraction_numerical': 0.0011266336187471834, 'mean_word_length': 4.1496519721577725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1646282', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1486, 'n_words': 321}