@@ -533,53 +490,17 @@ class Register extends React.Component {
);
}
- _renderCodeWaiting() {
- const { state, } = this;
-
- return (
-
- );
+ onTelegramAuth = (user, x) => {
+ // dataAuthUrl='/api/reg/modal/telegram/callback'
+ console.log(user, x)
+ /*const response = await callApi('/api/reg/modal/telegram/callback');
+ if (response.ok || response.status === 400) {
+ const result = await response.json();
+ console.log(result)
+ }*/
}
- _renderSocialButtons() {
+ _renderSocialButtons(showTitle = true, center = true) {
const { config } = this.state;
if (!config || !config.grants) {
return null;
@@ -592,11 +513,20 @@ class Register extends React.Component {
const facebook = hasGrant('facebook');
const yandex = hasGrant('yandex');
const mailru = hasGrant('mailru');
- const empty = !vk && !facebook && !yandex && !mailru;
+ const telegram = hasGrant('telegram')
+ const empty = !vk && !facebook && !yandex && !mailru && !telegram
+
+ if (!this.state.authType && !empty) {
+ const { dailyLimit } = this.props
+ if (dailyLimit && dailyLimit.exceed) {
+ return null
+ }
+ }
return (
-
- {!this.state.authType && !empty && tt('register_jsx.or_use_socsite')}
+
+ {showTitle && !this.state.authType && !empty && tt('register_jsx.or_use_socsite')}
+ {showTitle ?
: null}
{vk &&
@@ -617,6 +547,15 @@ class Register extends React.Component {
}
+ {telegram && !showTitle &&
+ {tt('register_jsx.or_use_telegram')}
+
}
+ {telegram &&
+
+
}
);
}
@@ -682,7 +621,7 @@ class Register extends React.Component {
_onSubmit = async e => {
e.preventDefault();
this.setState({ submitting: true });
- const { email, invite_code, name, password, passwordValid, referrer, recaptcha_v2, } = this.state;
+ const { verificationWay, invite_code, name, password, passwordValid, referrer, recaptcha_v2, } = this.state;
if (!name || !password || !passwordValid) return;
let publicKeys;
@@ -707,8 +646,7 @@ class Register extends React.Component {
try {
// create account
const res = await callApi('/api/reg/submit', {
- email: email !== '' ? email : undefined,
- invite_code: email === '' ? invite_code : undefined,
+ invite_code: verificationWay === 'invite_code' ? invite_code : undefined,
name,
owner_key: publicKeys[0],
active_key: publicKeys[1],
@@ -755,30 +693,6 @@ class Register extends React.Component {
this.setState({ code });
};
- validateEmail = (value, isFinal) => {
- const { config, } = this.state;
-
- const fakeEmailsAllowed = config && config.fake_emails_allowed;
-
- let emailError = null;
- let emailHint = null;
-
- if (!value) {
- emailError = tt('mobilevalidation_js.email_cannot_be_empty');
- } else if (!fakeEmailsAllowed && !/^[a-z0-9](\.?[a-z0-9]){5,}@g(oogle)?mail\.com$/.test(value)) {
- emailError = tt('mobilevalidation_js.email_must_be_gmail');
- }
-
- if (emailError) {
- emailError =
- '' + emailError;
- } else {
- emailHint = 'Google email: ' + value;
- }
-
- this.setState({ emailError, emailHint });
- };
-
validateInviteCode = async (value, isFinal) => {
let inviteError = null;
let inviteHint = null;
@@ -830,7 +744,7 @@ class Register extends React.Component {
if (error_str) {
newState.message = error_str;
}
- if (verification_way === 'email' && step === 'verified') {
+ if (verification_way === 'social' && step === 'verified') {
newState.message = tt(
'register_jsx.phone_number_has_been_verified'
);
@@ -839,44 +753,10 @@ class Register extends React.Component {
this.setState(newState, after);
}
- onClickSelectAnotherPhone = () => {
- this.setState({
- fetching: false,
- step: 'sending',
- });
- };
-
- onClickSendCode = async () => {
- const { email } = this.state;
-
- this.setState({
- fetching: true,
- });
-
- try {
- const res = await callApi('/api/reg/send_code', {
- email
- });
-
- let data = await res.json();
-
- this.updateApiState(data);
- } catch (err) {
- console.error('Caught /send_code server error', err);
-
- this.updateApiState({
- status: 'err',
- error_str: err.message ? err.message : err,
- });
- }
- };
-
onClickContinueInvite = async () => {
this.setState({
fetching: true,
message: '',
-
- email: '',
});
const res = await callApi('/api/reg/use_invite', {
@@ -888,35 +768,6 @@ class Register extends React.Component {
this.updateApiState(data);
};
- onCheckCode = async () => {
- try {
- const res = await callApi('/api/reg/verify_code', {
- confirmation_code: this.state.code,
- email: this.state.email
- });
-
- let data = await res.json();
-
- this.updateApiState(data);
- } catch (err) {
- console.error('Caught /verify_code server error:', err);
- this.updateApiState({
- status: 'err',
- error_str: err.message ? err.message : err,
- });
- }
- };
-
- onEmailChange = e => {
- // продолжаем let
- let email = e.target.value.trim().toLowerCase()
- this.validateEmail(email)
-
- this.setState({
- email
- });
- };
-
onInviteCodeChange = e => {
// продолжаем let
let invite_code = e.target.value.trim()
diff --git a/src/server/passport.js b/src/server/passport.js
index 4565f11..a03c381 100644
--- a/src/server/passport.js
+++ b/src/server/passport.js
@@ -5,6 +5,7 @@ import { Strategy as VKontakteStrategy, } from 'passport-vk';
import { Strategy as FacebookStrategy, } from 'passport-facebook';
import { Strategy as MailruStrategy, } from 'passport-mail';
import { Strategy as YandexStrategy, } from 'passport-yandex';
+import { TelegramStrategy } from 'passport-telegram-official'
import { throwErr, } from '@/server/error'
import Tarantool from '@/server/tarantool';
import { getRemoteIp, } from '@/server/misc';
@@ -32,56 +33,76 @@ export const checkAlreadyUsed = async (req, verifyType, verifyId, errBodyProps =
const strategies = {
vk: VKontakteStrategy, facebook: FacebookStrategy,
- mailru: MailruStrategy, yandex: YandexStrategy
+ mailru: MailruStrategy, yandex: YandexStrategy,
+ telegram: TelegramStrategy
};
for (const [grantId, grant] of Object.entries(config.grant)) {
const strategy = strategies[grantId];
if (!strategy || !grant.enabled) continue;
try {
- passport.use(new strategy(
- {
- clientID: grant.key,
- clientSecret: grant.secret,
- callbackURL: `${config.rest_api}/api/reg/modal/${grantId}/callback`,
- passReqToCallback: true
- },
- async (req, accessToken, refreshToken, params, profile, done) => {
- try {
- delete req.session.soc_error
- req.session.soc_id = profile.id;
- req.session.soc_id_type = grantId + '_id';
+ let opts
+ const verifyCallback = async (req, profile, done) => {
+ console.log('TS callback')
+ console.log(profile)
+ try {
+ delete req.session.soc_error
+ req.session.soc_id = profile.id;
+ req.session.soc_id_type = grantId + '_id';
- const idHash = hash.sha256(req.session.soc_id.toString(), 'hex');
+ const idHash = hash.sha256(req.session.soc_id.toString(), 'hex');
- console.log('-- social existing id');
+ console.log('-- social existing id');
- await checkAlreadyUsed(req, 'social-' + grantId, idHash)
+ await checkAlreadyUsed(req, 'social-' + grantId, idHash)
- console.log('-- social select user');
+ console.log('-- social select user');
- let user = await Tarantool.instance('tarantool').select('users', 'by_verify_uid',
- 1, 0, 'eq', ['social-' + grantId, idHash, req.session.uid]);
+ let user = await Tarantool.instance('tarantool').select('users', 'by_verify_uid',
+ 1, 0, 'eq', ['social-' + grantId, idHash, req.session.uid]);
- if (!user[0]) {
- console.log('-- social insert user');
- user = await Tarantool.instance('tarantool').insert('users',
- [null, req.session.uid, 'social-' + grantId, idHash, true, '1234', getRemoteIp(req), false]);
- }
+ if (!user[0]) {
+ console.log('-- social insert user');
+ user = await Tarantool.instance('tarantool').insert('users',
+ [null, req.session.uid, 'social-' + grantId, idHash, true, '1234', getRemoteIp(req), false]);
+ }
- req.session.user = user[0][0];
+ req.session.user = user[0][0];
- await req.session.save();
- } catch (err) {
- console.error('social error:', err)
+ await req.session.save();
+ } catch (err) {
+ console.error('social error:', err)
- req.session.soc_error = err
- delete req.session.soc_id
- req.session.soc_id_type = grantId + '_id'
+ req.session.soc_error = err
+ delete req.session.soc_id
+ req.session.soc_id_type = grantId + '_id'
- await req.session.save()
- }
- done(null, {profile});
+ await req.session.save()
+ }
+ done(null, {profile});
+ }
+
+ let callbackWrapper
+ if (grantId === 'telegram') {
+ opts = {
+ botToken: grant.bot_token,
+ passReqToCallback: true,
}
+ callbackWrapper = verifyCallback
+ } else {
+ opts = {
+ clientID: grant.key,
+ clientSecret: grant.secret,
+ callbackURL: `${config.rest_api}/api/reg/modal/${grantId}/callback`,
+ }
+ callbackWrapper = async (req, accessToken, refreshToken, params, profile, done) => {
+ return await verifyCallback(req, profile, done)
+ }
+ }
+ passport.use(new strategy(
+ {
+ ...opts,
+ },
+ callbackWrapper
));
} catch (ex) {
console.error(`ERROR: Wrong config.grant.${grantId} settings. Fix them or just disable registration with ${grantId}. Error is following:`)
@@ -123,6 +144,12 @@ export const addModalRoutes = (handler) => {
failureRedirect: '/api/reg/modal/failure'
}))
+ .get('/api/reg/modal/telegram', authenticator('telegram'))
+ .get('/api/reg/modal/telegram/callback', passport.authenticate('telegram', {
+ successRedirect: '/api/reg/modal/success',
+ failureRedirect: '/api/reg/modal/failure'
+ }))
+
.get('/api/reg/modal/success', (req, res) => {
res.status(200)
.setHeader('Content-Type', 'text/html; charset=utf-8')
diff --git a/yarn.lock b/yarn.lock
index 344eaad..92846be 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4401,11 +4401,18 @@ passport-oauth@1.0.x:
passport-oauth1 "1.x.x"
passport-oauth2 "1.x.x"
-passport-strategy@1.x.x:
+passport-strategy@1.x.x, passport-strategy@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
integrity sha1-tVOaqPwiWj0a0XlHbd8ja0QPUuQ=
+passport-telegram-official@^2.0.1:
+ version "2.0.1"
+ resolved "https://registry.yarnpkg.com/passport-telegram-official/-/passport-telegram-official-2.0.1.tgz#c54bd5c82aab904c2e4466a280806c19da94366e"
+ integrity sha512-h7ugNskZjkouF4HuOeYs25bmAg/Ct7jcOTaB1lZgWPXAX8SL9NRp11+h1b0udpKodYRgnFfR5tS6IummI0qVLQ==
+ dependencies:
+ passport-strategy "^1.0.0"
+
passport-vk@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/passport-vk/-/passport-vk-1.0.0.tgz#acdb98a06149d7ff2bddf7b6f0c8fa54319ff32f"
@@ -4616,6 +4623,15 @@ prop-types@^15.5.0, prop-types@^15.5.10, prop-types@^15.7.2:
object-assign "^4.1.1"
react-is "^16.8.1"
+prop-types@^15.6.2:
+ version "15.8.1"
+ resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
+ integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==
+ dependencies:
+ loose-envify "^1.4.0"
+ object-assign "^4.1.1"
+ react-is "^16.13.1"
+
psl@^1.1.28:
version "1.8.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
@@ -4801,7 +4817,7 @@ react-is@17.0.2, react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
-react-is@^16.3.2, react-is@^16.7.0, react-is@^16.8.1:
+react-is@^16.13.1, react-is@^16.3.2, react-is@^16.7.0, react-is@^16.8.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
@@ -4822,6 +4838,22 @@ react-refresh@0.8.3:
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.8.3.tgz#721d4657672d400c5e3c75d063c4a85fb2d5d68f"
integrity sha512-X8jZHc7nCMjaCqoU+V2I0cOhNW+QMBwSUkeXnTi8IPe6zaRWfn60ZzvFDZqWPfmSJfjub7dDW1SP0jaHWLu/hg==
+react-telegram-login@^1.1.2:
+ version "1.1.2"
+ resolved "https://registry.yarnpkg.com/react-telegram-login/-/react-telegram-login-1.1.2.tgz#28b9bdd68bb2710afca19354ac1f9428092836f0"
+ integrity sha512-pDP+bvfaklWgnK5O6yvZnIwgky0nnYUU6Zhk0EjdMSkPsLQoOzZRsXIoZnbxyBXhi7346bsxMH+EwwJPTxClDw==
+ dependencies:
+ react "^16.13.1"
+
+react@^16.13.1:
+ version "16.14.0"
+ resolved "https://registry.yarnpkg.com/react/-/react-16.14.0.tgz#94d776ddd0aaa37da3eda8fc5b6b18a4c9a3114d"
+ integrity sha512-0X2CImDkJGApiAlcf0ODKIneSwBPhqJawOa5wCtKbu7ZECrmS26NvtSILynQ66cgkT/RJ4LidJOc3bUESwmU8g==
+ dependencies:
+ loose-envify "^1.1.0"
+ object-assign "^4.1.1"
+ prop-types "^15.6.2"
+
react@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"