Search in sources :

Example 11 with WxOpenId

use of com.stardata.starshop2.authcontext.domain.user.WxOpenId in project starshop by beautautumn.

the class StarshopAuthLoginTests method should_load_exists_user_correctly_by_openid.

// 1.3. 根据openid查找用户记录;(相关任务,资源库端口,访问数据库)
@Test
@Transactional
@Rollback(true)
void should_load_exists_user_correctly_by_openid() {
    // given: 创建新用户,并设置其openid后保存
    WxOpenId openId = WxOpenId.of("testOpenIdX");
    User user = User.of("testUserX", 1);
    user.setOpenid(openId);
    userRepository.add(user);
    // when: 向资源库加入该用户,并重新从资源库根据openid重建用户对象
    User loadedUser = userRepository.findByOpenId(openId);
    // then: 重建的用户对象存在且OpenId等于给定的openid
    assertNotNull(loadedUser);
    assertEquals(openId, loadedUser.getOpenid());
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with WxOpenId

use of com.stardata.starshop2.authcontext.domain.user.WxOpenId in project starshop by beautautumn.

the class WxLoginWithTokenService method loginWithToken.

public User loginWithToken(String code, WxAuthInfo wxAuthInfo, User loginUser) {
    WxOpenId openid = wxLoginService.wxLogin(code, wxAuthInfo);
    User user = userExistenceService.ensureUser(openid, loginUser);
    userTokenService.generateLoginToken(user, wxAuthInfo.getSessionKey());
    return user;
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User) WxOpenId(com.stardata.starshop2.authcontext.domain.user.WxOpenId)

Example 13 with WxOpenId

use of com.stardata.starshop2.authcontext.domain.user.WxOpenId in project starshop by beautautumn.

the class UserExistenceService method ensureUser.

public User ensureUser(WxOpenId openId, User userToEnsure) {
    User user = userRepository.findByOpenId(openId);
    if (user == null) {
        userToEnsure.setOpenid(openId);
        user = userToEnsure;
        userRepository.add(user);
    } else {
        user.copyMiniAppInfoFrom(userToEnsure);
        userRepository.update(user);
    }
    return user;
}
Also used : User(com.stardata.starshop2.authcontext.domain.user.User)

Aggregations

WxOpenId (com.stardata.starshop2.authcontext.domain.user.WxOpenId)12 Test (org.junit.jupiter.api.Test)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)11 User (com.stardata.starshop2.authcontext.domain.user.User)9 WxAuthInfo (com.stardata.starshop2.authcontext.domain.user.WxAuthInfo)6 Rollback (org.springframework.test.annotation.Rollback)6 Transactional (org.springframework.transaction.annotation.Transactional)6 UserToken (com.stardata.starshop2.authcontext.domain.user.UserToken)2 LoginLog (com.stardata.starshop2.authcontext.domain.loginlog.LoginLog)1 LocalDateTime (java.time.LocalDateTime)1