How to use the @loopback/authentication.authenticate function in @loopback/authentication

To help you get started, we’ve selected a few @loopback/authentication examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / labs / authentication-passport / src / __tests__ / acceptance / authentication-with-passport-strategy-adapter.acceptance.ts View on Github external
description: '',
            schema: {
              type: 'string',
            },
          },
        },
      })
      .build();

    @api(apispec)
    class MyController {
      constructor(
        @inject(AuthenticationBindings.CURRENT_USER) private user: UserProfile,
      ) {}

      @authenticate(AUTH_STRATEGY_NAME)
      async whoAmI(): Promise {
        return this.user.id;
      }
    }
    app.controller(MyController);
  }
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / update-character.controller.ts View on Github external
char.currentMana! = char.maxMana!;
    char.attack! += 3 * levels;
    char.defence! += levels;
    await this.characterRepository!.updateById(currentUser.email, char);
    return char;
  }

  @patch('/updatecharacter/initCharacter', {
    responses: {
      '200': {
        description: 'initCharacter',
        content: {},
      },
    },
  })
  @authenticate('jwt', {
    required: [PermissionKey.ViewOwnUser, PermissionKey.UpdateOwnUser],
  })
  async initCharacter(@requestBody() newChar: NewChar): Promise {
    const currentUser = await this.getCurrentUser();
    //equip new weapon
    let char: Character = await this.characterRepository.findById(
      currentUser.email,
    );
    console.log(newChar);
    char.attack! += newChar.gear.weapon.attack!;
    char.defence! += newChar.gear.weapon.defence!;
    char.attack! += newChar.gear.armor.attack!;
    char.defence! += newChar.gear.armor.defence!;
    char.name = newChar.name;
    await this.characterRepository.updateById(currentUser.email, char);
    //console.log(await this.characterRepository.findById(currentUser.email));
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / update-character.controller.ts View on Github external
.create(weapon);
  }

  /**
   * update armor for current character
   * @param armor armor
   */
  @patch('/updatecharacter/armor', {
    responses: {
      '200': {
        description: 'update armor',
        content: {'application/json': {schema: Armor}},
      },
    },
  })
  @authenticate('jwt', {
    required: [PermissionKey.ViewOwnUser, PermissionKey.UpdateOwnUser],
  })
  async updateArmor(@requestBody() armor: Armor): Promise {
    const currentUser = await this.getCurrentUser();
    //equip new armor
    let char: Character = await this.characterRepository.findById(
      currentUser.email,
    );
    char.attack! += armor.attack;
    char.defence! += armor.defence;

    //unequip old armor
    let filter: Filter = {where: {characterId: currentUser.email}};
    if ((await this.armorRepository.find(filter))[0] != undefined) {
      let oldArmor: Armor = await this.characterRepository
        .armor(currentUser.email)
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / admin.controller.ts View on Github external
): Promise {
    return await this.characterRepository.findById(email);
  }

  /**
   * patch character by email
   * @param where filter
   */
  @patch('/admin/characters/{email}', {
    responses: {
      '204': {
        description: 'Character PATCH success',
      },
    },
  })
  @authenticate('jwt', {"required": [PermissionKey.ViewAnyUser, PermissionKey.UpdateAnyUser]})
  async updateById(
    @param.query.string('email') email: string,
    @requestBody() character: Character,
  ): Promise {
    await this.characterRepository.updateById(email, character);
  }

  /**
   * delete character by email
   */
  @del('/admin/characters/{email}', {
    responses: {
      '204': {
        description: 'Character DELETE success',
      },
    },
github strongloop / loopback4-example-shopping / packages / shopping / src / controllers / user-order.controller.ts View on Github external
return this.userRepo.orders(userId).create(order);
  }

  @get('/users/{userId}/orders', {
    responses: {
      '200': {
        description: "Array of User's Orders",
        content: {
          'application/json': {
            schema: {type: 'array', items: {'x-ts-type': Order}},
          },
        },
      },
    },
  })
  @authenticate('jwt')
  @authorize({resource: 'order', scopes: ['find'], voters: [compareId]})
  async findOrders(
    @param.path.string('userId') userId: string,
    @param.query.string('filter') filter?: Filter,
  ): Promise {
    const orders = await this.userRepo.orders(userId).find(filter);
    return orders;
  }

  @patch('/users/{userId}/orders', {
    responses: {
      '200': {
        description: 'User.Order PATCH success count',
        content: {'application/json': {schema: CountSchema}},
      },
    },
github strongloop / loopback4-example-shopping / packages / shopping / src / controllers / user-order.controller.ts View on Github external
@param.path.string('userId') userId: string,
    @param.query.string('filter') filter?: Filter,
  ): Promise {
    const orders = await this.userRepo.orders(userId).find(filter);
    return orders;
  }

  @patch('/users/{userId}/orders', {
    responses: {
      '200': {
        description: 'User.Order PATCH success count',
        content: {'application/json': {schema: CountSchema}},
      },
    },
  })
  @authenticate('jwt')
  @authorize({resource: 'order', scopes: ['patch'], voters: [compareId]})
  async patchOrders(
    @param.path.string('userId') userId: string,
    @requestBody() order: Partial,
    @param.query.string('where') where?: Where,
  ): Promise {
    return this.userRepo.orders(userId).patch(order, where);
  }

  @del('/users/{userId}/orders', {
    responses: {
      '200': {
        description: 'User.Order DELETE success count',
        content: {'application/json': {schema: CountSchema}},
      },
    },
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / character.controller.ts View on Github external
async printCurrentUser(): Promise {
    return this.getCurrentUser();
  }

  /**
   * show current character
   */
  @patch('/characters/name', {
    responses: {
      '200': {
        description: 'Character model instance',
        content: {'application/json': {schema: {'x-ts-type': Character}}},
      },
    },
  })
  @authenticate('jwt', {required: [PermissionKey.ViewOwnUser]})
  async changeName(@requestBody() newName: Partial): Promise {
    const currentUser = await this.getCurrentUser();
    let char: Character = await this.characterRepository.findById(
      currentUser.email,
    );
    char.name = newName.name!;
    return await this.characterRepository.updateById(currentUser.email, char);
  }

  /**
   * show current character
   */
  @get('/characters', {
    responses: {
      '200': {
        description: 'Character model instance',
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / admin.controller.ts View on Github external
return await this.characterRepository.find(filter);
  }

  /**
   * path all character
   * @param where filter
   */
  @patch('/admin/characters', {
    responses: {
      '200': {
        description: 'Character PATCH success count',
        content: {'application/json': {schema: CountSchema}},
      },
    },
  })
  @authenticate('jwt', {"required": [PermissionKey.ViewAnyUser, PermissionKey.UpdateAnyUser]})
  async updateAll(
    @requestBody() character: Character,
    @param.query.object('where', getWhereSchemaFor(Character)) where?: Where,
  ): Promise {
    return await this.characterRepository.updateAll(character, where);
  }

  /**
   * show character by email
   * @param email email
   */
  @get('/admin/characters/{email}', {
    responses: {
      '200': {
        description: 'Character model instance',
        content: {'application/json': {schema: {'x-ts-type': Character}}},
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / admin.controller.ts View on Github external
@requestBody() character: Character,
  ): Promise {
    await this.characterRepository.updateById(email, character);
  }

  /**
   * delete character by email
   */
  @del('/admin/characters/{email}', {
    responses: {
      '204': {
        description: 'Character DELETE success',
      },
    },
  })
  @authenticate('jwt', {"required": [PermissionKey.ViewAnyUser, PermissionKey.DeleteAnyUser]})
  async deleteById(
    @param.path.string('email') email: string
  ): Promise {
    //delete weapon, armor, and skill
    await this.characterRepository.weapon(email).delete();
    await this.characterRepository.armor(email).delete();
    await this.characterRepository.skill(email).delete();
    ///
    await this.characterRepository.deleteById(email);
  }
}
github gobackhuoxing / first-web-game-lb4 / firstgame / src / controllers / character.controller.ts View on Github external
async findById(): Promise {
    const currentUser = await this.getCurrentUser();
    return await this.characterRepository.findById(currentUser.email);
  }

  /**
   * delete current character
   */
  @del('/characters', {
    responses: {
      '204': {
        description: 'Character DELETE success',
      },
    },
  })
  @authenticate('jwt', {required: [PermissionKey.DeleteOwnUser]})
  async deleteById(): Promise {
    const currentUser = await this.getCurrentUser();
    //delete weapon, armor, and skill
    await this.characterRepository.weapon(currentUser.email).delete();
    await this.characterRepository.armor(currentUser.email).delete();
    await this.characterRepository.skill(currentUser.email).delete();
    ///
    await this.characterRepository.deleteById(currentUser.email);
  }
}