74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { NatsService } from 'app/core/nats/services/nats.service';
|
|
import * as nats from 'nats.ws';
|
|
|
|
import { Error } from 'app/modules/protobuf/protobuf/rpc/error_pb';
|
|
|
|
import {
|
|
CheckUsernameForDuplicationRequest,
|
|
CheckUsernameForDuplicationResponse,
|
|
CaptchaRequest,
|
|
CaptchaResponse,
|
|
} from 'app/modules/protobuf/c2se/common/identity_pb';
|
|
import {
|
|
SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
|
|
SUBJECT_CAPTCHA,
|
|
} from 'app/modules/protobuf/c2se/backend/identity_pb';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class IdentityService {
|
|
/**
|
|
* Constructor
|
|
*/
|
|
constructor(private __natsService: NatsService) {}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Accessors
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Public methods
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
checkUsernameForDuplication(username: string): Promise<boolean> {
|
|
return new Promise<boolean>((resolve, reject) => {
|
|
let req = new CheckUsernameForDuplicationRequest();
|
|
req.setUsername(username);
|
|
|
|
this.__natsService
|
|
.request<CheckUsernameForDuplicationResponse.Result>(
|
|
SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
|
|
req.serializeBinary(),
|
|
CheckUsernameForDuplicationResponse.deserializeBinary
|
|
)
|
|
.then((result) => {
|
|
console.log('success', result, result.getDuplicated());
|
|
})
|
|
.catch((e: Error) => {
|
|
console.log('failed', e.getCode(), e.getMessage(), e.getData());
|
|
});
|
|
});
|
|
}
|
|
|
|
captcha(): Promise<boolean> {
|
|
return new Promise<boolean>((resolve, reject) => {
|
|
let req = new CaptchaRequest();
|
|
|
|
this.__natsService
|
|
.request<CaptchaResponse.Result>(
|
|
SUBJECT_CAPTCHA,
|
|
req.serializeBinary(),
|
|
CaptchaResponse.deserializeBinary
|
|
)
|
|
.then((result) => {
|
|
console.log('success', result, result.getToken(), result.getImage());
|
|
})
|
|
.catch((e: Error) => {
|
|
console.log('failed', e.getCode(), e.getMessage(), e.getData());
|
|
});
|
|
});
|
|
}
|
|
}
|