All files / app/service auth.ts

17.97% Statements 16/89
0% Branches 0/2
0% Functions 0/13
17.97% Lines 16/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 17883x                         83x   83x   82x                                       82x                                           82x                                                                     82x                       82x                       82x             82x                     82x               82x             82x               82x           82x           82x            
import { BoolValue } from "google-protobuf/google/protobuf/wrappers_pb";
import { HostingStatus } from "proto/api_pb";
import {
  AntiBotReq,
  ConfirmDeleteAccountReq,
  ContributorForm as ContributorFormPb,
  GetInviteCodeInfoReq,
  RecoverAccountReq,
  SignupAccount,
  SignupBasic,
  SignupFlowReq,
  UnsubscribeReq,
  UsernameValidReq,
} from "proto/auth_pb";
 
import client from "./client";
 
export async function startSignup(
  name: string,
  email: string,
  inviteCode?: string,
) {
  const req = new SignupFlowReq();
  const basic = new SignupBasic();
 
  Iif (inviteCode) {
    basic.setInviteCode(inviteCode);
  }
 
  basic.setName(name);
  basic.setEmail(email);
 
  req.setBasic(basic);
  const res = await client.auth.signupFlow(req);
  return res.toObject();
}
 
export async function getInviteCodeInfo(code: string) {
  const req = new GetInviteCodeInfoReq();
  req.setCode(code);
  const res = await client.auth.getInviteCodeInfo(req);
  return res.toObject();
}
 
interface AccountSignupData {
  flowToken: string;
  username: string;
  password?: string;
  birthdate: string;
  gender: string;
  acceptTOS: boolean;
  optOutOfNewsletter: boolean;
  hostingStatus: HostingStatus;
  city: string;
  lat: number;
  lng: number;
  radius: number;
}
 
export async function signupFlowAccount({
  flowToken,
  username,
  password,
  birthdate,
  gender,
  acceptTOS,
  optOutOfNewsletter,
  hostingStatus,
  city,
  lat,
  lng,
  radius,
}: AccountSignupData) {
  const req = new SignupFlowReq();
  req.setFlowToken(flowToken);
  const account = new SignupAccount();
  account.setUsername(username);
  account.setBirthdate(birthdate);
  account.setGender(gender);
  account.setAcceptTos(acceptTOS);
  account.setOptOutOfNewsletter(optOutOfNewsletter);
  account.setHostingStatus(hostingStatus);
  account.setCity(city);
  account.setLat(lat);
  account.setLng(lng);
  account.setRadius(radius);
  Iif (password) {
    account.setPassword(password);
  }
  req.setAccount(account);
  const res = await client.auth.signupFlow(req);
  return res.toObject();
}
 
export function contributorFormFromObject(form: ContributorFormPb.AsObject) {
  const formData = new ContributorFormPb();
  formData
    .setIdeas(form.ideas)
    .setFeatures(form.features)
    .setExperience(form.experience)
    .setContribute(form.contribute)
    .setContributeWaysList(form.contributeWaysList)
    .setExpertise(form.expertise);
  return formData;
}
 
export async function signupFlowFeedback(
  flowToken: string,
  form: ContributorFormPb.AsObject,
) {
  const req = new SignupFlowReq();
  req.setFlowToken(flowToken);
  const formData = contributorFormFromObject(form);
  req.setFeedback(formData);
  const res = await client.auth.signupFlow(req);
  return res.toObject();
}
 
export async function signupFlowEmailToken(emailToken: string) {
  const req = new SignupFlowReq();
  req.setEmailToken(emailToken);
  const res = await client.auth.signupFlow(req);
  return res.toObject();
}
 
export async function signupFlowCommunityGuidelines(
  flowToken: string,
  accept: boolean,
) {
  const req = new SignupFlowReq();
  req.setFlowToken(flowToken);
  req.setAcceptCommunityGuidelines(new BoolValue().setValue(accept));
  const res = await client.auth.signupFlow(req);
  return res.toObject();
}
 
export async function signupFlowResendVerificationEmail(flowToken: string) {
  const req = new SignupFlowReq();
  req.setFlowToken(flowToken);
  req.setResendVerificationEmail(true);
  const res = await client.auth.signupFlow(req);
  return res.toObject();
}
 
export async function validateUsername(username: string) {
  const req = new UsernameValidReq();
  req.setUsername(username);
  const res = await client.auth.usernameValid(req);
  return res.getValid();
}
 
export async function unsubscribe(payload: string, sig: string) {
  const req = new UnsubscribeReq();
  req.setPayload(payload);
  req.setSig(sig);
  const res = await client.auth.unsubscribe(req);
  return res.toObject();
}
 
export async function confirmDeleteAccount(token: string) {
  const req = new ConfirmDeleteAccountReq();
  req.setToken(token);
  await client.auth.confirmDeleteAccount(req);
}
 
export async function recoverAccount(token: string) {
  const req = new RecoverAccountReq();
  req.setToken(token);
  await client.auth.recoverAccount(req);
}
 
export async function antibot(token: string, action: string) {
  const req = new AntiBotReq();
  req.setToken(token);
  req.setAction(action);
  return await client.auth.antiBot(req);
}